471,050 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,050 software developers and data experts.

Validate number with JS?

Hello there,

It's possible to check if it's a valid number in <INPUT...

I try typeof and parseFloat and it's not working. It's seem impossible to
test (with a if) the value "NaN"!!

Regards

Jul 20 '05 #1
4 20177
Hi,

Jeff wrote:
Hello there,

It's possible to check if it's a valid number in <INPUT...

I try typeof and parseFloat and it's not working. It's seem impossible to
test (with a if) the value "NaN"!!

Regards


var strValue = document.formName.textFieldName.value;
var fValue = parseFloat( strValue );

if ( isNaN( fValue ) )
{
document.formName.textFieldName.focus();
document.formName.textFieldName.select();
alert( "Please enter a valid number!" );
}

HTH,

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #2
"Jeff" <pr*******@hotmail.com> writes:
It's possible to check if it's a valid number in <INPUT...
Good.
I try typeof and parseFloat and it's not working. It's seem impossible to
test (with a if) the value "NaN"!!


What is a valid number? You need to answer that before coding anything.

If a valid number is just a sequence of digits, i.e., only a positive
integer, then the answer is different from if a valid number can be
a negative fractional number in engineering notation (e.g., -2.4E-10).

Just checking against NaN ought to be simple, but it isn't. For some
reason (NaN === NaN) is false. NaN is not equal to any number value,
not even itself. Spooky. Luckily, there is a native function for just
that comparison:

if (!isNaN(parseFloat(formRef.elements['field'].value))) { ... }

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Lee
Jeff said:

Hello there,

It's possible to check if it's a valid number in <INPUT...

I try typeof and parseFloat and it's not working. It's seem impossible to
test (with a if) the value "NaN"!!


if(isNaN(myValue)){
alert("That's not a number!");
}

<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/toplev.html#1064024>

Jul 20 '05 #4
JRS: In article <nN*********************@wagner.videotron.net>, seen in
news:comp.lang.javascript, Jeff <pr*******@hotmail.com> posted at Sat,
11 Oct 2003 14:18:47 :-
It's possible to check if it's a valid number in <INPUT...

I try typeof and parseFloat and it's not working. It's seem impossible to
test (with a if) the value "NaN"!!


There is another trap with isNaN(). AIUI, it accepts a too-big number
which means infinity. Infinity is unlikely to be valid in most
applications.

Is the number to be integer? positive? non-negative? if 22 is allowed,
do you need also to accept 2.2e1? How about 022 - is it allowed, and is
its value twenty-two or eighteen? How about 0XED for 237? - or, indeed,
CCXXXVII? If 022 is allowed, hoe about 000000000000000000000000000022?

For the most probable answers to those questions, you would do well to
check the string that <INPUT... "has" in .value with a RegExp. When the
test is passed, convert to number with a unary +, as in the FAQ, 4.21;
and then, if necessary, test the numeric value to be within range.

--
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 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andrew | last post: by
3 posts views Thread by Mike P | last post: by
3 posts views Thread by Poppy | last post: by
11 posts views Thread by jjbutera | last post: by
2 posts views Thread by Neo Geshel | last post: by
4 posts views Thread by Brybot | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.