I have used this rule to validate the user billing address if he wants a bill (the input #billyes is checked), this is my 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 is repeated too the line return $('#billyes').is(":checked");
I want to do something like this:
//BILLING VALIDATION
legal_id,billing_name,billing_adress,etc: {
required: {
depends: function(element) {
return $('#billyes').is(":checked");
}
}
}
Is this possible? Is there any alternative?
Thanks you.
Within the
.validate()method, you cannot do anything like that at all. The object literal within therulesoption must bekey:valuepairs where thekeycan only be a single fieldname.However, there is a way to apply the same rule to multiple fields at once:
You can combine the
.rules()method with a jQuery.each(). But you'll need a clever jQuery selector to target them all at once. Like a "starts with" selector or aclass. You can't use "starts withbilling_" since you also havelegal_id, so you'll need to assign aclassto these fields or do something else.Otherwise, no, within the
.validate()method, there is no shortcut.