简体   繁体   中英

JQuery Validation: How to add custom rule to two input fields but with different name value?

I use JQuery Validation( https://jqueryvalidation.org/ ) to validation my form. I write custom rule and use the rule to validation the input field.

jQuery.validator.addMethod("onlyoneuser", function (value, element) {
        var username = /^[^,,\s]*$/;
        return this.optional(element) || username.test(value);
    }, "只允许填写一位指派者");

    form.validate({
        errorPlacement: function errorPlacement(error, element) {
            error.insertAfter(element.parent());
        },

        rules: {
            "aonep-roject": {
                projectcheck: true,
            },
            "onlyoneuser": {
                onlyoneuser: true,
            }
        },

    });

I would like to use the same rule to validate two input fields. But the two fields have different name values, like . How to apply the sample to these two fields ? Thank you very much!

onlyoneuser: true The true is param give onlyoneuser method.

 function (value, element,param) 
{
    //param == true
    //value is filed value
    //element is input
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM