2

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.

1
  • Instead of writing a complex function for the required rule, look into creating a whole new custom method with the addMethod method. Commented Sep 26, 2013 at 2:36

1 Answer 1

4
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.

Sign up to request clarification or add additional context in comments.

4 Comments

I have a jobType with a value of '0' and another with a value of '1' that don't validate using this method. All the other jobTypes (2-7) work just fine. Any idea how you would solve this?
I appreciate the update, however, this results in the same behavior as the original approach. Everything works correctly except for jobType 0 and jobType 1.
I'll test it a little further when I get home from work... busy day today :D. Only have about and hour left lol.
Just wanted to check back on this. This approach still isn't correctly validating for 0 and 1. Everything else validates correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.