0

I am reading a value as

 var result = $("#Time", $(this)).val();

If I enter "Hours" instead of a number , and I do 0<parseInt(result, 10)<24), the result is true when I expect it to be false.

What is wrong with my code?

1
  • 1
    Note that (once you take David's advice, below) you may want 0 <= rather than 0 <... Commented Oct 22, 2012 at 7:24

1 Answer 1

3

Your first comparison 0 < NaN will yield false, which is sort of 0, therefore 0 < 24, which is true.

var result = parseInt($('#Time', $(this)).val(), 10);
var between0and24 = 0 < result && result < 24

I also note that you seem to be passing context incorrectly. The context parameter to jQuery should be a DOM node, not a jQuery object, so use $('#Time', this). Note that there's no use for that parameter in this code as accessing by ID is really quick as it is, and because you aren't using the Time id for more than one element in the document, right? Well you shouldn't.

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

2 Comments

How can I achieve the result to false?
@learning: By usign the code in my answer. changing from x < y < z to x < y && y < z.

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.