Page 1 of 1

Restricting what can be entered into a text field.

Posted: Tue Jul 10, 2007 1:07 pm
by bos45430
I would like to know how to restrict users from entering anything but alphanumeric characters and no spaces into a textfield.

Thanks

Posted: Tue Jul 10, 2007 1:55 pm
by dirkr
well with php you can do a check after they submit.
Dont know if you have ever heard of regular expressions, but in my opinion.. thats the way to do it.

ereg function
Some info about regular expressions

they'll be able to enter whatever they want into the field..but after they submit you check it and if its wrong, just redirect them back to the form.

If you want to do it on input instantly then i think you're gonna have to think about javascript or ajax

Posted: Tue Jul 10, 2007 2:28 pm
by RobertGonzalez
ctype_alnum()... no regex needed.

Posted: Tue Jul 10, 2007 2:37 pm
by feyd
It's not a good idea to recommend ereg. Why? It's being removed from PHP. If regular expressions is a must, use preg.

Posted: Tue Jul 10, 2007 11:57 pm
by bos45430
perfect! thanks!

john

Posted: Wed Jul 11, 2007 12:42 am
by Benjamin
If spaces are ok use this instead.

Code: Select all

preg_match('#^[a-z0-9\s]+$#i', $string);