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.
I hope that you have enjoyed reading over our examples, and we hope they have helped you get a better grasp of validating a JavaScript array. Validation will keep your code free of errors and unexpected crashes.
Another way to make the jQuery validate plugin also detect all the inputs beyond the first could be by making the names unique. So you could replace: <input type="text" name="qty []" /> <input type="text" name="qty []" /> <input type="text" name="qty []" /> <input type="text" name="qty []" />
Form validation is a process of confirming the relevant information entered by the user in the input field. Here we will be validating a simple form that consists of a username, password and a confirm password using jQuery. Prerequisites: You must be aware of the basics of HTML, CSS, JavaScript, and jQuery.
JQUERY: VALIDATE MULTIPLE FIELDS WITH THE SAME NAME Sometimes we need to validate an array of input elements: For example – Now we will use jquery validation plugin jquery.validate.js for validating the form. The condition will be that user will have to choose field_name from each dropdown.
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();
}
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