Forum Moderators: open

Message Too Old, No Replies

Resrtict to Alpha/Numberic

         

Acternaweb

7:43 pm on Apr 5, 2005 (gmt 0)

10+ Year Member



I have a form that I want to restrict what the user can put in it, for example phone number can only use numeric characters. name would only have alpha. is this complex?

thanks,

Bernard Marx

10:53 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not too complex with regular expressions. REs can get tricky, but you might only need the basics:

// with numValue, alphaValue variables holding our input strings..

isNumeric = /^[0-9]+$/.test(numValue);
isAlpha = /^[a-z]+$/i.test(alphaValue);

[] - single character selection
^ - beginning of string
$ - end of string
+ - 1 or more of (ie [a-z]+ means: a, b, ac, aaf, etc..)
i - ignore case

Concerning the all-alpha value, this is OK for us anglos, men talar man ett annat språk, så behöver man mera bokstäver.

You might need to consider whether you need to include 'foreign' characters, not just [a-z]