views:

58

answers:

2
 var val= $('#num').val(); // 01
     if(isNaN(val) || val > 59 || val== ''){
   alert("error");
 }

In this case, has not alert error

       var val= $('#num').val(); // 01
       val = parseInt(val);
+1  A: 

Well you code has no val in it, guessing it is a typo.

isNaN(val) <-- checking to see if a string is a number?

val > 59 <-- comparing string vs a number. NOT a number vs Number

val== '' <-- seeing if a string is matching nothing

You want to use parseInt() for the second check. Read the linked page.

epascarello
Don't forget about sending the radix with `parseInt()` i.e. `parseInt('08', 10)`. Or use `Number()`.
alex
thank you ! convert string to number
BRAVO
A: 

You need to:

  • Define a variable name: var yourVariable = "01";

  • Then use typeof to find its type: typeof(yourVariable);

typeof will return "string", in my example. It can also return "number" or "undefined" - very useful.

Steve
I wouldn't recommend naming variables like that - they look like global constants (and thus could be confusing). Also, `typeof` is a unary operator, not a function :)
alex
You're right, I was trying to draw attention to it. Changed. Now, to research unary operators :)
Steve