Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Validation for Array of Input Elements

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.

like image 528
eddy Avatar asked Dec 24 '10 12:12

eddy


People also ask

Why validate an array in JavaScript?

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.

How to make the jQuery validate plugin also detect all inputs?

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 []" />

What is form validation in jQuery?

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.

How to validate multiple fields with the same name in 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.


1 Answers

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();
    }
like image 163
eddy Avatar answered Oct 11 '22 13:10

eddy