Jquery Validation On Select Box Not Working December 21, 2023 Post a Comment I am using jQuery validation plugin for client side validation, but my validation does not work on my select box. HTML Solution 1: A simple way to fix this problem is to give the non valid option the value of "". Then simply call validate on your form and it will not submit when "Choose" is selected.HTML<formid="formid"><selectname="select"class="required"><optionvalue="">Choose</option><optionvalue="child">test2</option></select><inputtype="submit" /></form> CopyJavaScript$("#formid").validate(); CopyDemoSolution 2: Although this probably works with some of the aforementioned methods,if you're looking to use a custom validation function, you should use addMethod as documented here: http://docs.jquery.com/Plugins/Validation/Validator/addMethodSo you would first add the method through something like$.validator.addMethod("requiredSelect", function(element) { return ( $("#select").val() !='-1' ); }, "You must select an option."); CopyThen simply assign the validator with$("#formid").validate({rules: { select: { requiredSelect :true } } });CopySolution 3: instead of:$("#select").val()try:$("#select :selected").val()$("#select").val() returns all the option values instead of the selected one.Here, my assumption is that you want to check if the user has chosen the option -1 when the control report-crime is validated.Solution 4: by default<option value="">Choose</option>works withrequired: trueSolution 5: There is missing name attribute in your select element. In my case that was the issue since the jQuery Validatation Plugin looks for the name not id while validating. Share Post a Comment for "Jquery Validation On Select Box Not Working"
Post a Comment for "Jquery Validation On Select Box Not Working"