I-M-JM I-M-JM - 3 months ago 23
jQuery Question

jQuery validate select box

I am using jQuery Validation Plugin for form validation and I am not able to validate list/select box

<select id="select">
<option value="-1">Choose an option</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>

Now, I want to make sure that the user selects anything but "Choose an option" (which is the default one, and has value as -1). So that it won't validate if you choose the first option. How can this be done?


You can use Jquery Validator custom rule.

A similar example is discussed here. The code below is from the example:

// add the rule here
 $.validator.addMethod("valueNotEquals", function(value, element, arg){
  return arg != value;
 }, "Value must not equal arg.");

 // configure your validation
  rules: {
   SelectName: { valueNotEquals: "-1" }
  messages: {
   SelectName: { valueNotEquals: "Please select an item!" }