Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make select2 work with jquery.validation plugin?

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

like image 364
user2323711 Avatar asked May 23 '14 09:05

user2323711


People also ask

Is Select2 jQuery?

Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.

How we can use jQuery validation plugins in MVC?

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.

Does jQuery validate require a form?

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.

What is remote jQuery validation?

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.


1 Answers

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   } }); 
like image 115
ABIRAMAN Avatar answered Sep 20 '22 05:09

ABIRAMAN