Check if part of string is a number

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
ace2600
Forum Commoner
Posts: 30
Joined: Fri Jun 21, 2002 12:12 am

Check if part of string is a number

Post by ace2600 »

I'm a newbie to javascipt.
When a user submits a form I want to check one of the characters with in a field to see if it is a number, just one character though, not the entire field. Heres what I do now, its sad I know.

Code: Select all

if(checkStr.charAt(0) != 0 && checkStr.charAt(0) != 1...)
and I do this until I get to 9. There has to be a better way, please tell me what it is.
Thanks,
Ace
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

charCodeAt() ( from '0' --> 48 to '9' -> 57)
parseInt()
Number()
RegEx

i.e.

Code: Select all

if (checkStr.charCodeAt(0) > 47 && checkStr.charCodeAt(0) < 58)
Post Reply