jQuery Validation - Multiple submit buttons on one asp.net form, different validation groups?

前端 未结 6 1743
时光取名叫无心
时光取名叫无心 2020-12-07 06:01

I have an asp.net form with a login section and a register section. There are two submit buttons that correspond to the appropriate section, login or register. I am using

6条回答
  •  粉色の甜心
    2020-12-07 07:04

    Keep track of the validation object and remove/replace rules directly. Works for me with v1.13.0

    var validation = $('#form1').validate(); //Keep track of validation object
    
    //Rule set 1
    var validationRulesLogin = {
        headerEmail: {
            required: true,
            email: true
        },
        headerPassword: "required"
    };
    var validationMessagesLogin = {
        headerEmail: {
            required: "Please enter your email address.",
            email: "Not a valid email address."
        },
        headerPassword: "Please enter your password."
    };
    
    //Rule set 2
    var validationRulesSignup = {
        signupEmail: {
            required: true,
            email: true
        },
        signupPassword: "required",
        signupPassword2: {
            equalTo: "#phBody_txtNewPassword"
        }
    };
    var validationMessagesSignup = {
        signupEmail: {
            required: "Please enter your email address.",
            email: "Not a valid email address."
        },
        signupPassword: "Please enter your password.",
        signupPassword2: "Passwords are not the same."
    };
    
    //Toggle rule sets on form focus, button click or other event.
    function validatingLoginForm(){
        validation.resetForm();
        validation.settings.rules = validationRulesLogin;
        validation.settings.messages = validationMessagesLogin;
    }
    function validationSignupForm(){
        validation.resetForm();
        validation.settings.rules = validationRulesSignup;
        validation.settings.messages = validationMessagesSignup;
    }
    

提交回复
热议问题