jQuery Validation plugin: disable validation for specified submit buttons

后端 未结 12 603
故里飘歌
故里飘歌 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:39

    I found that the most flexible way is to do use JQuery's:

    event.preventDefault():
    

    E.g. if instead of submitting I want to redirect, I can do:

    $("#redirectButton").click(function( event ) {
        event.preventDefault();
        window.location.href='http://www.skip-submit.com';
    });
    

    or I can send the data to a different endpoint (e.g. if I want to change the action):

    $("#saveButton").click(function( event ) {
        event.preventDefault();
        var postData = $('#myForm').serialize();
        var jqxhr = $.post('http://www.another-end-point.com', postData ,function() {
        }).done(function() {
            alert("Data sent!");
        }).fail(function(jqXHR, textStatus, errorThrown) {
            alert("Ooops, we have an error");
        })
    

    Once you do 'event.preventDefault();' you bypass validation.

    0 讨论(0)
  • 2020-11-22 16:42

    (Extension of @lepe's and @redsquare answer for ASP.NET MVC + jquery.validate.unobtrusive.js)


    The jquery validation plugin (not the Microsoft unobtrusive one) allows you to put a .cancel class on your submit button to bypass validation completely (as shown in accepted answer).

     To skip validation while still using a submit-button, add a class="cancel" to that input.
    
      <input type="submit" name="submit" value="Submit"/>
      <input type="submit" class="cancel" name="cancel" value="Cancel"/>
    

    (don't confuse this with type='reset' which is something completely different)

    Unfortunately the jquery.validation.unobtrusive.js validation handling (ASP.NET MVC) code kinda screws up the jquery.validate plugin's default behavior.

    This is what I came up with to allow you to put .cancel on the submit button as shown above. If Microsoft ever 'fixes' this then you can just remvoe this code.

        // restore behavior of .cancel from jquery validate to allow submit button 
        // to automatically bypass all jquery validation
        $(document).on('click', 'input[type=image].cancel,input[type=submit].cancel', function (evt)
        {
            // find parent form, cancel validation and submit it
            // cancelSubmit just prevents jQuery validation from kicking in
            $(this).closest('form').data("validator").cancelSubmit = true;
            $(this).closest('form').submit();
            return false;
        });
    

    Note: If at first try it appears that this isn't working - make sure you're not roundtripping to the server and seeing a server generated page with errors. You'll need to bypass validation on the server side by some other means - this just allows the form to be submitted client side without errors (the alternative would be adding .ignore attributes to everything in your form).

    (Note: you may need to add button to the selector if you're using buttons to submit)

    0 讨论(0)
  • 2020-11-22 16:42
    $("form").validate().settings.ignore = "*";
    

    Or

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

    But without success in a custom required validator. For call a submit dynamically, i have created a fake hidden submit button with this code:

    var btn = form.children('input.cancel.fakeSubmitFormButton');
    if (btn.length === 0) {
        btn = $('<input name="FakeCancelSubmitButton" class="cancel fakeSubmitFormButton hide" type="submit" formnovalidate value="FakeCancelSubmitButton" />');
        form.append(btn);
    }
    btn.click();
    

    Now skip the validation correctly :)

    0 讨论(0)
  • 2020-11-22 16:43
    <button type="submit" formnovalidate="formnovalidate">submit</button>
    

    also working

    0 讨论(0)
  • 2020-11-22 16:46

    Add formnovalidate attribute to input

        <input type="submit" name="go" value="Submit"> 
        <input type="submit" formnovalidate name="cancel" value="Cancel"> 
    

    Adding class="cancel" is now deprecated

    See docs for Skipping validation on submit on this link

    0 讨论(0)
  • 2020-11-22 16:46

    This question is old, but I found another way around it is to use $('#formId')[0].submit(), which gets the dom element instead of the jQuery object, thus bypassing any validation hooks. This button submits the parent form that contains the input.

    <input type='button' value='SubmitWithoutValidation' onclick='$(this).closest('form')[0].submit()'/>
    

    Also, make sure you don't have any input's named "submit", or it overrides the function named submit.

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