JQuery Validation for Array of Input Elements
19,361
Solution 1
In jquery.validate.js, we can find a function named checkForm, we have to modify it as below:
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();
}
Solution 2
Based on eddy answer, this function takes into count also the ignore
setting.
checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
var checkingElements = this.findByName( elements[i].name ).not(this.settings.ignore);
if (checkingElements.length !== undefined && checkingElements.length > 1) {
for (var cnt = 0; cnt < checkingElements.length; cnt++) {
this.check( checkingElements[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
},
Solution 3
You have to loop through in these cases, like this:
$(document).ready(function() {
$("#form1").validate({
submitHandler: function(form) {
form.submit();
}
});
$("#form1 input[name='mileage']").each(function() {
$(this).rules("add", { required: true });
});
});
.rules()
only affects the first match, so you need a .each()
on there to loop through and add any rules to all matches.
Related videos on Youtube
Author by
eddy
Updated on June 04, 2022Comments
-
eddy almost 2 years
I need to validate an array of input text elements (mileage): For example:
<tbody> <c:forEach items="${list}" var="item"> <tr> <!--some other columns---> <td align="left"><input type="text" name="mileage" value="" /></td> </tr> </c:forEach> </tbody>
The script for validation is as below -
$(document).ready(function(){ $("#form1").validate({ rules: { mileage: { required: true } }, submitHandler: function(form) { form.submit(); } }); });
Now the problem is that the .validate.js only validates the first element of mileage. What can I do? How can I make the plugin validate all of the inputs text ?
I hope you can help me out.
-
eddy over 13 years@Nick Craver. Thank you I tried what you suggested, but it still doesn't work :(. Are you sure that's all I need to do?? :(
-
Nick Craver over 13 years@eddy - yup that's all that's needed...are you getting any JavaScript errors in your console?
-
Luca Fagioli over 11 yearsExcellent indeed. But I have found that this function does not take into count the "ignore" property. I have just refined the function in my answer. Hope this helps.
-
Stephan Møller about 11 yearsI prefer not to manipulate plugin source code manually but instead include my custom hot fixed in a seperate .js file, which I then include on the page after jquery.validate.js. Is there a way this can be done with the checkForm-method in the extension?
-
Luca Fagioli about 11 yearsI have forked the project on github and did a pull request, so hopefully you will find this update directly in a new version of jquery.validate.js