473,386 Members | 1,819 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,386 software developers and data experts.

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 11805
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
2
by: Stephan Walter | last post by:
Hi, I'm hacking on a LISP interpreter in JS , and running into trouble with numbers and Number objects. This is what I have: var a = new Number(1); var b = a; document.writeln(a == b); ...
4
by: John Vottero | last post by:
When a class contains a TimeSpan property, XmlSerializer doesn't work. A TimeSpan property is serialized like: <MySpan /> I've read a number of posts that talk about why this happens and how...
3
by: Shapper | last post by:
Hello, I have a database table with 2 fields: and ; I need to create a function which increases by 1 the number in field for a certain id. In this moment I have the following: Private...
2
by: Alex Buell | last post by:
Is there an elegant way of converting strings containing digits between different number bases in C++? I.e.: 10 (base 2) = 2 (base 10) FF (base 16) = 256 (base 10) F (base 16) = 1111 (base 2)...
12
by: Sheau Wei | last post by:
1. let say my table in MySQL have a column call NUMBER. In this column it would be value 1,2,3. Now i want to make the 1,2,3 as option to user to choose. what the code should be? 2. is it this...
28
by: Tony Johansson | last post by:
Hello! If I write this statement int number = new int(); I don't get compiler error or run time error but how will the compiler read such a statement ? For example will number be a reference...
2
Claus Mygind
by: Claus Mygind | last post by:
Using the following coding <input type text id="myField" value ="" /> var cThisVal = parseFloat( document.getElementById("myField").value) When I check the value of "cThisVal" and see...
1
jinalpatel
by: jinalpatel | last post by:
I am dealing with 3 tables. Here is a brief meta data see the attachment for the relationship Conditions: 1)One GrantIndex can have multiple Deliverables 2)one Deliverable is made from...
4
subedimite
by: subedimite | last post by:
Here I am with another question being a very new VBA user. I have this scenario to work with: I would think this is fairly common routine of work for VBA. I have a string of hex contents as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
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...

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.