Stopping execution of jquery.validate
March 24th, 2008
Jörn Zaefferer’s jQuery Plugin is very easy to start for all the forms you have on a page -
if ($(':input').length > 0) { $('form').each(function(obj){
$(this).validate();
}
}
…so long as you have the right includes, like jquery.metadata, it works regardless of form name.
The problem is getting it to stop, under conditions you control.
function validateMe () { return $(this).validate().form(); }
$(':submit[name=submit][value=Cancel Order]‘).bind(’click’,function(e){
if(confirm(”Are you sure you want to cancel this order?”)) {
$(this).parents(’form’).unbind(’submit’,validateMe);
return true;
}
return false;
});
if ($(’:input’).length > 0) {
$(’form’).each(function(obj){
$(this).bind(’submit’,validateMe);
});
}
The main idea here is that all functions associated with ‘bind’ can be affected with an ‘unbind’ statement - if the functions are named.
Note the use of validate().form() - this derivation returns a boolean, rather than doing all the bind operations on its own.
Posted in JavaScript |