How to add 'depends:' rule to many fields in jQuery validation plugin? -
i have used rule validate user billing address if wants bill (the input #billyes checked), current code:
//billing validation legal_id: { required: { depends: function(element) { return $('#billyes').is(":checked"); } } }, billing_name: { required: { depends: function(element) { return $('#billyes').is(":checked"); } } }, billing_adress: { required: { depends: function(element) { return $('#billyes').is(":checked"); } } }, .... etc it repeated line return $('#billyes').is(":checked");
i want this:
//billing validation legal_id,billing_name,billing_adress,etc: { required: { depends: function(element) { return $('#billyes').is(":checked"); } } } is possible? there alternative?
thanks you.
legal_id,billing_name,billing_adress,etc: { within .validate() method, cannot @ all. object literal within rules option must key:value pairs key can single field name.
however, there way apply same rule multiple fields @ once:
you can combine .rules() method jquery .each(). you'll need clever jquery selector target them @ once. "starts with" selector or class. can't use "starts billing_" since have legal_id, you'll need assign class these fields or else.
$('.yourfields').each(function() { $(this).rules('add', { required: { depends: function(element) { return $('#billyes').is(":checked"); } } }); }); otherwise, no, within .validate() method, there no shortcut.
Comments
Post a Comment