Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

64 of 85 people (75%) answered Yes
Recently 6 of 10 people (60%) answered Yes


how can I do a javascript edit check for floating point number on a form.

Feb 24th, 2009 23:22
haytham ali, ha mo, Iván Rivera, Robin Jaggassar,

You only have to remember that JavaScript sees all values of form
elements as text strings. The easiest way to check if a certain string
is a floating point number is applying to it the parseFloat() global
function, which returns the floating point number that corresponds to a
given string (parseFloat('42.4242') returns 42.4242 as a number), or NaN
(not a number) if the string given is not a valid representation for a
floating point number.
The global function isNaN() helps also, determining if a given number is
NaN or some other thing (a real number, infinite or minus infinite). So
your validation code could look like this:
  if (isNaN(parseFloat(myString))) alert('Not a number');
There is also a isInfinite() global function to help with the infinite
case, working the same way than isNaN().
I hope this was useful...