JQuery validate--Validation for multiple elements of the same name _jquery

Source: Internet
Author: User

In project development, you typically encounter a form of a table, then there may be several elements in the form of the same name, in this case, under the Validate default condition is only to verify the first, and the following elements can not be validated, then there are two ways to solve this problem.

Note: Regardless of the following approach, elements of the same name must have an ID, otherwise there will be no effect.

Method One:

Add the following code to the JS file of the page you want to verify:

/** * Processing for validation of elements with the same name/$ (function () {if ($.validator) {$.validator.prototype.elements = function () {

            var validator = this, Rulescache = {};
                Select all valid inputs inside the form (no submit or Reset buttons) return $ (this.currentform) . Find ("Input, select, textarea"). Not (": Submit,: RESET,: Image, [disabled]"). Not (thi S.settings.ignore). Filter (function () {if!this.name && validator.settings.
                    Debug && window.console) {console.error ("%o has no name assigned", this); }//Note This line of code//select only the ' the ' the ' only ' for each name, and only those WI
                    TH rules specified//if (this.name in Rulescache | |!validator.objectlength $ (this). Rules ()) {
                    return false;
        //}            Rulescache[this.name] = true;
                return true;
        }); }
    }
});
Method Two: Modify JQuery.validate.js source code

Elements:function () {
            var validator = this,
                Rulescache = {};

            Select all valid inputs inside the form (no submit or Reset buttons) return
            $ (this.currentform)
            . Find ("input, S Elect, textarea ")
            . Not (": Submit,: RESET,: Image, [disabled] ")
            . Not (This.settings.ignore)
            . Filter ( function () {
                if (!this.name && validator.settings.debug && window.console) {
                    Console.error (" %o has no name assigned ", this);
                }

                Select only the ' the ' the ' only ' for each name, and only those with the rules specified
                //Note if (THIS.name in Rulescache || !validator.objectlength ($ (this). Rules ())} {
                //return    	false;     }

                Rulescache[this.name] = true;
                return true;
            };
        
As you can see from the above code, you just need to comment out the IF statement at the callout.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.