How do I remove jQuery validation from a form?

Viewed 83411

I'm using the jQuery validation plugin to validate a form, and I'd like to remove the validation and submit the form if a certain link is clicked.

I am submitting form with javascript like jQuery('form#listing').submit(), so I must remove the validation rules/function with javascript.

The problem is that I can't figure out how to do this. I've tried things like jQuery('form#listing').validate({}); and jQuery('form#listing').validate = null, but with no luck.

11 Answers

Trigger the DOM submit method to skip the validation:

$("#listing")[0].submit();

You can remove events of nodes with unbind:

jQuery('form#listing').unbind('submit'); // remove all submit handlers of the form

What you are probably looking for is that the validation plugin can also unassign itself from the submit event:

jQuery('form#listing').validate({
   onsubmit : false
});

For both of these you should be able to follow up with a call to .submit() to submit the form:

jQuery('form#listing').unbind('submit').submit();
var form = $('#my_form_id').get(0);
$.removeData(form,'validator');

Is really working.

You can add a css class of cancel to an element (button, input) so that it skips the validation

This seems to work for me now:

var form = $('#my_form_id').get(0);
$(form).removeData('validate');

I recently upgraded to 1.5.5 of Jörn's validator script and the removeValidator function has been deprecated.


...
jQuery.extend(
          jQuery.fn, 
          {
          removeValidator: function(){
              this.unbind();
              jQuery.removeData(this[0], 'validator'); 
          }
...

in 2022

the answers above do not work now for me,

So I've written this js method, this would remove the jquery validation correctly

function RemoveJQVRule(rulename, inputname) {
   $(`[name="${inputname}"]`).rules('remove', rulename);
   $(`[name="${inputname}"]`).removeAttr(`data-val-${rulename}`);

   // message span element
   $(`#${inputname.replace(/\./img, '_')}-error`).html("");
   $(`[data-valmsg-for="${inputname}"]`).html("");
}

Examples:

    RemoveJQVRule('required', 'Shipper.Contact.NationalId');
    RemoveJQVRule('maxlength-max', 'SomeInputName');
    RemoveJQVRule('regex', 'SomeInputName');

Note:- if it does not work you may need to edit it a little bit (the css selectors)

Thanks

Related