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 ");

}
ozil
  • 6,930
  • 9
  • 33
  • 56
  • 2
    http://stackoverflow.com/questions/1779013/check-if-string-contains-only-digits or http://stackoverflow.com/questions/175739/is-there-a-built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number or http://stackoverflow.com/questions/1303646/check-whether-variable-is-number-or-string-in-javascript – epascarello Jul 29 '15 at 14:27
  • 3
    `NaN == NaN` results in `false`. Use the `isNaN` function. – Ram Jul 29 '15 at 14:28
  • parseInt itself returns an integer or NaN – ssilas777 Jul 29 '15 at 14:29
  • The `parseInt` is unnecessary; all you need is `if (isNaN(check)) {...}` – Daniel Beck Jul 29 '15 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`. – Matt Burland Jul 29 '15 at 14:34
  • @DanielBeck: Also note, `isNaN("")` is false, but `parseInt("",10)` returns `NaN` – Matt Burland Jul 29 '15 at 14:38
  • Thanks for All. My Problem is solved –  Jul 29 '15 at 15:44

1 Answers1

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.

ralh
  • 2,514
  • 1
  • 13
  • 19
  • *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`. – Matt Burland Jul 29 '15 at 14:41
  • I tried in several ways but did not worked for me thanks it solved my problem –  Jul 29 '15 at 14:45