Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Validation of Input array in jQuery

Hello Friends,

I was working with jQuery validation since long long time and I never came across such situation like today I faced. I want to validate the array of input element like name[]. I put required class but it was working with first element only. I tried with couple of other options but none of them works for me. Finally i have started writing code in jQuery validation file. It is working fine now. If you want to validate input array elements than just follow below steps.

Open jQuery.validate.js file and search checkForm:function() and replace it with below function.

 
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();
}

Now use below code to validate your input array elements using jQuery. You can change the name as per your page.

<script language="javascript">
$(document).ready(function() {
    // validate the comment form when it is submitted
    $("#frm").validate({
 
        rules: {
            "name[]": "required"
        },
        messages: {
            "name[]": "Please enter name",
        }
    });
});
</script>

Validate Input array elements jQuery, Validate Input array jQuery, How to validate Input array jQuery Validation, jQuery Validation Input array, jQuery input array validate, function for input array validation, input array validate jQuery.

Share:

2 Comments

Leave a Reply

Your email address will not be published.