jQuery Validation plugin: disable validation for specified submit buttons

后端 未结 12 604
故里飘歌
故里飘歌 2020-11-22 16:14

I have a form with multiple fields that I\'m validating (some with methods added for custom validation) with Jörn Zaeffere\'s excellent jQuery Validation plugin. How do you

相关标签:
12条回答
  • 2020-11-22 16:48

    You can use the onsubmit:false option (see documentation) when wiring up validation which will not validate on submission of the form. And then in your asp:button add an OnClientClick= $('#aspnetForm').valid(); to explicitly check if form is valid.

    You could call this the opt-in model, instead of the opt-out described above.

    Note, I am also using jquery validation with ASP.NET WebForms. There are some issues to navigate but once you get through them, the user experience is very good.

    0 讨论(0)
  • I have two button for form submission, button named save and exit bypasses the validation :

    $('.save_exist').on('click', function (event) {
                $('#MyformID').removeData('validator');
                $('.form-control').removeClass('error');
                $('.form-control').removeClass('required');                
                $("#loanApplication").validate().cancelSubmit = true;
                $('#loanApplication').submit();
                event.preventDefault();
    });
    
    0 讨论(0)
  • 2020-11-22 16:57

    Here is the simplest version, hope it helps someone,

    $('#cancel-button').click(function() {
        var $form = $(this).closest('form');
        $form.find('*[data-validation]').attr('data-validation', null);
        $form.get(0).submit();
    });
    
    0 讨论(0)
  • 2020-11-22 16:59

    Other (undocumented) way to do it, is to call:

    $("form").validate().cancelSubmit = true;
    

    on the click event of the button (for example).

    0 讨论(0)
  • 2020-11-22 17:00

    You can add a CSS class of cancel to a submit button to suppress the validation

    e.g

    <input class="cancel" type="submit" value="Save" />
    

    See the jQuery Validator documentation of this feature here: Skipping validation on submit


    EDIT:

    The above technique has been deprecated and replaced with the formnovalidate attribute.

    <input formnovalidate="formnovalidate" type="submit" value="Save" />
    
    0 讨论(0)
  • 2020-11-22 17:01

    Yet another (dynamic) way:

    $("form").validate().settings.ignore = "*";
    

    And to re-enable it, we just set back the default value:

    $("form").validate().settings.ignore = ":hidden";
    

    Source: https://github.com/jzaefferer/jquery-validation/issues/725#issuecomment-17601443

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