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

前端 未结 6 1755
时光取名叫无心
时光取名叫无心 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 06:37

    I'm guessing you are using this validation plugin: JQuery Validation . The plugin uses the form tag as a validation group so to accomplish what you want you will need to remove rules depending on which button the user pushes. Also, you will need to add the rules back for the case where the validation fails then the user decides to click the other button.

    Here's an example:

    $(document).ready(function()
    {
        $("#form").validate({
            invalidHandler: addRules 
        });        
    
        addRules();        
    
        $("#submit1").click(function(){
            $(".group2").each(function(){
                $(this).rules("remove");
            });
        });
        $("#submit2").click(function(){
            $(".group1").each(function(){
                $(this).rules("remove");
            });
        });
    
    });
    var addRules = function(){
        $("#input1").rules("add", {required:true});
        $("#input2").rules("add", {required:true});
    }
    

    This approach allows you to add a class attribute (group1 or group2) to each of your inputs which acts as a validation group.

提交回复
热议问题