View Full Version : trapping NaN and Infinity

12-30-2005, 07:32 AM
How can I check to see if a textbox contains NaN or infinity?

if (t1.value=='NaN')

doesn't seem to do the trick

Philip M
12-30-2005, 08:21 AM
isNaN cannot be tested with the == equality operator as it is not a number.

Use if (isNAN.t1.value)) { // do something

12-30-2005, 08:24 PM
isNaN(t1.value), actually.

If you want to check for infinity as well:

var parsed = parseFloat(t1.value);

if (isNaN(parsed) || !isFinite(parsed)) {
// not a number, or +/- infinity
else {
// finite number

12-31-2005, 06:59 PM
Thank you for the replies. I'd be pulling my hair out if it weren't for you guys!

DHTML Kitchen
01-01-2006, 03:02 AM
isFinite is sufficient. isNaN is not needed.

isFinite(Number.NaN) // false.