473,550 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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"!!


Jul 20 '05 #1
4 20267

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"!!


var strValue = document.formNa me.textFieldNam e.value;
var fValue = parseFloat( strValue );

if ( isNaN( fValue ) )
document.formNa me.textFieldNam e.focus();
document.formNa me.textFieldNam e.select();
alert( "Please enter a valid number!" );


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*******@hotm ail.com> writes:
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"!!

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(parseFl oat(formRef.ele ments['field'].value))) { ... }

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
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(myValu e)){
alert("That's not a number!");

<http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/toplev.html#106 4024>

Jul 20 '05 #4
JRS: In article <nN************ *********@wagne r.videotron.net >, seen in
news:comp.lang. javascript, Jeff <pr*******@hotm ail.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

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 000000000000000 000000000000022 ?

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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.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

by: Andrew | last post by:
Hi, How can I validate a date, like 3/26/04, before submitted? Thanks for any idea. -- Andrew
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 six digits long. I have no problems with the Is_In_Format function (I found it on the web and am using it for other things). My problem is with...
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 the XML file in my C# program. My code is below : change.xml : <?xml version="1.0"?> <rsp-vcn...
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
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 in the validated event. Is there a way to know if all validated controls pass validation when the user clicks an OK button? In ASP.Net there's...
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 I am stumped as to how to validate them as a group (as one final validation). I need to check to see if all (at one time) are filled or empty. The...
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 figured since all the panel data is still being sent through the postbacks, instead of using Sessions, or HttpContext, I could just take the values...
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 validate letters and number. For example, for the employee number, I have to enter number only; for employee name, I have to enter letter only, ...
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 = e.keyCode if(((strkeyIE >= 48) && (strkeyIE <= 57 )) || (strkeyIE >= 40) && (strkeyIE <= 41 ) || (strkeyIE == 32) || (strkeyIE == 46)||(strkeyIE
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.