0

As following example

var m="hello" or var m="50"

check the string whether the string got Numeric Value or Alphabet's

i am using this but it is not working

var check=parseInt(m);

if(Number(check)==NaN)

{

   alert("this is Not a Number ");

 }  else{

   alert("This is Number ");

}
7
  • 2
    stackoverflow.com/questions/1779013/… or stackoverflow.com/questions/175739/… or stackoverflow.com/questions/1303646/… Commented Jul 29, 2015 at 14:27
  • 3
    NaN == NaN results in false. Use the isNaN function. Commented Jul 29, 2015 at 14:28
  • parseInt itself returns an integer or NaN Commented Jul 29, 2015 at 14:29
  • The parseInt is unnecessary; all you need is if (isNaN(check)) {...} Commented Jul 29, 2015 at 14:30
  • @DanielBeck: It actually depends on how strict you want to be. For example, is 0x16 a valid numeric string for you? If you mean to only look for valid decimal values then it's not. And to catch that you'd have to parseInt(m,10). Although actually, even that wouldn't work because it will parse up to the first non-numeric character and return 0. Commented Jul 29, 2015 at 14:34

1 Answer 1

0
if(isNaN(m)) {
   //is not a number
} else {
   //is a number
}

Should work. NaN == NaN actually returns false.

EDIT: As the comments above said, the parseInt is not actually necessary.

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

2 Comments

As the comments above said, the parseInt is not actually necessary.: Sometimes it is. It depends on exactly what you want to consider as a valid number. Is an empty string a number? Because isNaN("") is false, but parseInt("",10) returns NaN.
I tried in several ways but did not worked for me thanks it solved my problem