Solved

Checking if a string is numeric

Posted on 1999-01-01
4
229 Views
Last Modified: 2006-11-17
Can anyone tell me how to check if a string is numeric? The string I would be checking would only be 2 characters long.

Many thanks

Paul
0
Comment
Question by:paul_skin
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
jhurst earned 50 total points
ID: 1276250
The length does not matter:

x=parseInt(y);
will set x to the numeric value of the string y, if numeric, if not numeric then x will be set to 'Nan'.
0
 
LVL 4

Expert Comment

by:martinag
ID: 1276251
Use isNaN():
isNan(123) will return false
isNan("123") will return false
isNan("123a") will return true

Martin
0
 
LVL 4

Expert Comment

by:martinag
ID: 1276252
Actually, jhurst's solution works like this:
parseInt(123) will return 123
parseInt("123") will return 123
parseInt("123a") will return 123
parseInt("") will return NaN
parseInt("a") will return NaN

Martin
0
 
LVL 2

Expert Comment

by:UniDyne
ID: 7950864
You need to keep in mind that some implementations (such as Rhino) do not return NaN for parseInt(). In Rhino, parseInt() will still return numbers strings like "123a". isNaN() has worked for me under all JavaScript implementations and appears to be more like what you are looking for.

In Rhino, all of the following statements are true. As you can see, isNaN() is more reliable.

parseInt("123a") == 123
parseInt("234-567") == 234
parseInt("a123") == "NaN"
parseInt("a123") != NaN
isNaN("123a") == true
isNaN("a123") == true
isNaN("234-567") == true
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question