Home
TeamSite
Form validation - invalid characters...
therocket
Hello everyone (again),
I'd like to validate a form element and make sure that the user hasn't entered invalid characters. Invalid characters = everything that isn't a to z and 0 to 9.
Any ideas? I found some JS on the web but was unable to convert it and make it work.
Thanks!!
Cheers.
Find more posts tagged with
Comments
Adam Stoller
How about a regex? "^[a-z0-9]+$"
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
therocket
That's perfect... Last thing: how do I modify that to allow them to insert "-" and "_"?
therocket
Is this correct?
"^[a-z0-9][\w/_\.][-\w\.]+$"
JonathonG
Assuming you want upper and lower case letters (which the original regex doesn't assume, it only allows lower case letters), the regex becomes simply:
"^[\w-]+$"
\w indicates a word character which is defined to be letters, numbers, and underscores. Then you just need to also include the - character
If you don't want upper case, just add _ and - to the character class:
"^[a-z0-9_-]+$"
Jonathon
Independent Interwoven Contractor
therocket
Perfect! Thanks guys...
Cheers!