I'm trying to validate select2 field using jquey.validation plugin but nothing happens.
I want to make select required field.
I'm using this custom validation function:
$.validator.addMethod("requiredcountry", function(value, element, arg){ return arg != value; }, "Value must not equal arg.");
and this is the rule:
rules: { country: { required: true, requiredcountry: "" } }
What select2 field should I use for the rule to match?
I appreciate any idea how to make select2 working together with jquery.validation
10x
Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.
UnobtrusiveJavaScriptEnabled" property value back to false and create a new file in "Scripts" folder and name it "script-custom-validator. js". Add the Jquery validator code in it as shown below i.e. The above piece of code attaches my account register form with jQuery form validator by using form ID.
The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.
According to the jQuery validate documentation for remote: The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message.
Just add ignore: [],
in Your form validation function. It will enable hidden field validation.So You will get validation for Select 2
$("#ContactForm").validate({ ignore: [], rules: { //Rules }, messages: { //messages } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With