JQuery Validation for Array of Input Elements

前端 未结 4 599
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 06:50

I need to validate an array of input text elements (mileage): For example:


  
        

        
相关标签:
4条回答
  • 2020-12-09 07:08

    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();
        }
    
    0 讨论(0)
  • 2020-12-09 07:15

    Using jquery validate 1.19.1 and modifying the jquery.validate.js file on line 465, replacing with this function should be fine.

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

    I hope to be of help ;)

    0 讨论(0)
  • 2020-12-09 07:27

    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();
            },
    
    0 讨论(0)
  • 2020-12-09 07:27

    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.

    0 讨论(0)
提交回复
热议问题