1. Home
  2. Computing & Technology
  3. JavaScript

Numeric Testing

Join the Discussion

Questions? Comments?

There are a number of different ways to test if a variable contains a numeric value but the simplest way is to use parseInt if the field needs to be a whole number and parseFloat if the number can have decimal places.

The code to perform these tests looks like this:

if (mynum != parseInt(mynum))
alert(mynum + ' is not a whole number');

if (mynum == parseFloat(mynum))
alert(mynum + ' is numeric');
Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.