views:

4924

answers:

5

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.

+3  A: 

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

redsquare
I am submitting the form with javascript, so unfortunately that won't work.
Ben
Luckily I am not doing anything complicated so this worked for me. Thanks.
uriDium
+10  A: 

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();
Borgar
Only the unbind method worked for me. Using jQuery 1.3.2 and Validate 1.5.5
JavadocMD
+4  A: 

Trigger the DOM submit method to skip the validation:

$("#listing")[0].submit();
Jörn Zaefferer
A: 

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'); 
       }
...
kenitech
A: 

This seems to work for me now:


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

kenitech
meh.Now i'm using the method mentioned by Jorn above. I can use this when I want to bypass validation:<pre> bypassValidator = function(){ $("#btn-submit").bind('click', function(){ $('#myForm')[0].submit(); }); }</pre>
kenitech
@Kenitech: your approach here solved a slightly different problem for me. I am dynamically loading in different forms via Ajax and if the field name was the same the existing validation would apply to it unless I first ran the code above.
Damo