Page 1 of 1

Check if part of string is a number

Posted: Tue Oct 15, 2002 9:53 pm
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

Posted: Tue Oct 15, 2002 10:17 pm
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)