Reputation: 1161
I'm trying to validate a field with multiple conditions. I've got validation working with a single condition, but I'm not sure how to go about adding in addl conditions. Here's what I have so far:
priceEstimate: {
required: function() {
return $('#jobType').val() != '8';
}
}
I also need to make sure that the value of #jobType does not equal '9' or '10' either. I tried using an or operator, and that didn't seem to do the trick.
Any help would be greatly appreciated.
Upvotes: 2
Views: 3913
Reputation:
priceEstimate: {
required: function() {
var jobType = $('#jobType').val();
if (jobType < '8' && jobType > 10)
{
return true;
}else{
return false;
}
}
}
There are likely simpler ways to write it... but that will do ya. http://www.w3schools.com/js/js_comparisons.asp
In response to Jeremy's comment:
priceEstimate: {
required: function ()
{
var jobType = Number($('#jobType').val());
var _return = true;
switch (true)
{
case (jobType <= 1):
case (jobType >= 8 && jobType <= 10):
_return = false;
break;
}
return _return;
}
}
Ok, what we did here is a cascading switch. The expression is set to true, so it will run each case... we're then putting our logic in each individual case.
We know we don't want 1 or 0, so I have just have it set to false if it is equal to 1 or below, without a break in that case, it will simply run on to the next case and validate even further, so you'll want to try and keep the cases in order least -> greatest if nothing for the sake of your own sanity, lol.
Also I'm using Number() to flesh out the numeric value in the input, just in case, this way we also don't have to encapsulate all of our checks with quotes ('10') and they're treated like actual numbers and not just representations that may be translated into something that would fail the logic your striving for.
Upvotes: 4