473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 20260
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrew | last post by:
Hi, How can I validate a date, like 3/26/04, before submitted? Thanks for any idea. -- Andrew
6
by: Abby Lee | last post by:
I must confirm the user enters a value for each fund they need. I do not know how many fund entries there will be...it's expandable to handle each users needs. I must varify each fund they enter is...
3
by: Mike P | last post by:
I have an xml doucument that I want to validate with a XSD schema. I'm new to this and I don't know exactly how I make the references at the top of the documents so that the XSD file will validate...
3
by: Poppy | last post by:
I have a text field I want to validate. It needs to store a number e.g "23.5", "10000" Is there any isnum() method in .net. TIA
11
by: jjbutera | last post by:
I know how to use the ErrorProvider in my winforms..or do I? I validate the values and set the ErrorProvider in the validating event. If not valid, I set e.Cancel = True. I clear the ErrorProvider...
2
by: Neo Geshel | last post by:
Greetings, I have a form with a telephone field. It is very specific, as it has four text boxes - the country code, area code, prefix and suffix. I can validate each of them individually, but...
4
by: Brybot | last post by:
I have a form that i've split up into multiple asp:panels, each panel has a number of validators which work correctly. At on the last panel, i want to commit the data collected to a database. I...
1
by: hhhhhhhh | last post by:
Hello all, When I run the application, it requires to enter Employee number, social, and name. I can enter any letter and number to call employee number, social and name. I do not know how to...
5
by: Abhishek | last post by:
Hi this is my another validator in javascript to validate the Phone Number :-) <script language='javascript'> function funcCheckPhoneNumber(ctrtxtMobile,e){ if(window.event){ var strkeyIE =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.