Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Checking if a string is numeric

Posted on 1999-01-01
Medium Priority
234 Views
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
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

LVL 8

Accepted Solution

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

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

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

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

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to create an extensible mechanism for linked drop downs.
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
Course of the Month11 days, left to enroll

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

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