469,929 Members | 1,889 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

value is a number

I'm trying to test if a value is a number.

I thought I could do this:

var test='not_a_number';
var test2='4.00';

num_test=parseFloat(test);

if(num_test == 'NaN'){alert('that was not a number')}

But that doesn't work. (at least not in NS)

Maybe a regex?

Jeff


Jul 20 '05 #1
2 11601
> I'm trying to test if a value is a number.

I thought I could do this:

var test='not_a_number';
var test2='4.00';

num_test=parseFloat(test);

if(num_test == 'NaN'){alert('that was not a number')}

But that doesn't work. (at least not in NS)

Maybe a regex?


Use the isNaN() function, or better, use the isFinite() function.

if (!isFinite(test)) {alert(test + ' is not a number');}

NaN is a freaky thing. The comparison operators don't work with it.

http://www.crockford.com/javascript/survey.html
Jul 20 '05 #2
JRS: In article <QX*****************@newsread1.news.atl.earthlink. net>,
seen in news:comp.lang.javascript, Jeff Thies <no****@nospam.net> posted
at Tue, 18 Nov 2003 16:40:16 :-
I'm trying to test if a value is a number.

I thought I could do this:

var test='not_a_number';
var test2='4.00';

num_test=parseFloat(test);

if(num_test == 'NaN'){alert('that was not a number')}

But that doesn't work. (at least not in NS)
NaN is not a number; since this might occur in many ways, it is equal to
nothing, not even itself. But 'NaN' is a perfectly good non-numeric
string, and +'NaN' gives NaN without being equal to it.

Maybe a regex?


Yes. In any reasonable application in which it is right to validate a
number, it is likely that the set of suitable numbers is much smaller
than the set of possible numbers. Likewise formats.

You may in practice want non-negative integers, with no need to allow
the format 1e2; in that case test with RegExp /^\d+$/ - see
<URL:http://www.merlyn.demon.co.uk/js-maths.htm#Valid>.

Once the format is OK, you can convert the string to a number in safety
with unary +
Numeric = +Stringy.value
and then if necessary do further tests with arithmetic comparison.

E.G. : In US notation, seconds format is RegExp /^[0-5]\d$/ = 00..59 but
there is nothing quite so simple for hours 01..12 ; /^0[1-9]|1[0-2]$/ .

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by simondex | last post: by
2 posts views Thread by Stephan Walter | last post: by
4 posts views Thread by John Vottero | last post: by
3 posts views Thread by Shapper | last post: by
2 posts views Thread by Alex Buell | last post: by
28 posts views Thread by Tony Johansson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.