Reputation: 3060
Using jquery validate plugin I'm using the rule equalTo to validate a field as follows:
$('#booking').validate({
ignoreTitle: true,
rules: {
payments: {
equalTo: {
param: "#sales",
depends: function(element) { return $("#status").val() == 'invoiced'; }
}
},
}
});
I can get this to work but now I'd only like this rule to apply only if a select option is a particular value. I'm lost on this and not sure how to make the equalTo rule optional dependant on another field value
I saw depends rule but cant find any documentation on this
Can anyone give me some pointers please?
I;ve created a jsfiddle to help:
http://jsfiddle.net/longestdrive/SneAF/2/
Thanks
Updated: I've tried the depends but not working as expected:
Also found some similar/duplicate questions: jQuery Validation - require field only if another field is filled and jQuery validation depends
Upvotes: 2
Views: 5908
Reputation: 3955
There were some errors in your depends
-rule:
$("#status option:selected")
Invoiced
, not invoiced
This is the final result (As RobM mentioned, you could even specify more exact error messages:
$('#booking').validate({
ignoreTitle: true,
rules: {
payments: {
equalTo: {
param: '#sales',
depends: function(element) {
return $("#status option:selected").val() == 'invoiced';
}
}
}
},
messages: {
payments: {
equalTo: "Payments must equal Sales when Status is 'Invoiced'."
}
}
});
Upvotes: 4