?
Solved

Checking if a string is numeric

Posted on 1999-01-01
4
Medium Priority
?
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
jhurst earned 200 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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 discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

770 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