I have used the jquery validate plugin to check select box unique and required.
However, check unique is not working at all and check required is only work for the first select box ,
how to fix this ? Thank you
jquery
$("#insertResult").validate({
rules: {
'select[]': {
required: true,
unique: true
}
},
});
html
<form id="insertResult" method="post" action="excelSQL.php" >
<select id="selectField0" name="select[]" style="float:left">
<option selected="" value="">Select one</option>
<option value="Email">Email</option>
<option value="1_Name2">1_Name2</option>
</select>
<select id="selectField1" name="select[]" style="float:left">
<option selected="" value="">Select one</option>
<option value="Email">Email</option>
<option value="1_Name2">1_Name2</option>
</select>
</form>
Updated:
I have edited the js file which i have done before and i have tried selectField0 or selectField_0 , still no luck
checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
this.check( this.findByName( elements[i].name )[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
},
This is a common problem with the jQuery validator. There are two ways around this problem:
Method1:
Modify the jQuery Validator plug-in so that it handles both by following these instructions: http://www.codeboss.in/web-funda/2009/05/27/jquery-validation-for-array-of-input-elements/
Method2:
Apply the Validator to each select individually:
HTML:
JavaScript:
Method #3:
While jQuery plugins can be quite valuable for the more basic tasks, in some cases, what you’re trying to do may in fact be outside the scope of what the plug-in was designed to do. Whenever I hear solutions like “modify the library code”, it makes me think that perhaps that library is just not for me. This depends on the complexity of the modifications, and the likelihood that my coworkers or successors may have trouble figuring out why things stopped working when they updated the plugin to the latest version.
Thus, method #3 sidesteps the jQuery validator altogether and only uses jQuery to facilitate the basic validation:
JavaScript:
HTML:
The above code will submit the form if and only if the following conditions are true:
The selections are not the same and meets the “unique” condition.
When a user focuses the select box, the errors disappear.
If the user tries to submit without the above submit conditions being true, the following errors are shown:
If one or more select box is not selected, the “These are required fields” message shows.