jquery validator plugin - changing validation rules dynamically -


heres code im working with:

$('#button').click( function (e) {   var rules = rules_data; //object holds rules form validation    if (a == b) { //some random criteria      delete object.rule5; //deletes specific rule (in case 'rule5') out of object if criteria met, otherwise rules object unchanged     }    var validate_form = $('#form').validate(rules);    if (validate_form === false) {     e.preventdefault();   } }); 

my problem code - tho rules object changes supposed on each button click (if criteria met), validation takes rules of 1st click , gets stuck rules if rules changed. mean, if rules changed on button click, want validation discard old rules , apply new ones, not happening reason.

any suggestions? maybe im doing wrong here?

.validate() method of initializing plugin on form. can called once on page , if try call again, no matter options pass, nothing.

so you'll want remove .validate() click handler , put inside dom ready handler.

afaik, way change rules dynamically replace them using built-in rules('add') method.

if have rule input called field1, defined this...

rules: {     field1: {         required: true,         number: true     } } 

you change field's rules replacing them. use rules('add') method this...

$('input[name="field1"]').rules('add', {     required: true,     number: false,     minlength: 10 }); 

if want target many fields @ once same settings, you'll have incorporate jquery .each() method. this...

$('input').each(function() {     $(this).rules('add', {         required: true,         minlength: 10     }); }); 

Comments