Restricting what can be entered into a text field.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bos45430
Forum Newbie
Posts: 12
Joined: Sun Jul 08, 2007 11:11 am

Restricting what can be entered into a text field.

Post by bos45430 »

I would like to know how to restrict users from entering anything but alphanumeric characters and no spaces into a textfield.

Thanks
dirkr
Forum Newbie
Posts: 20
Joined: Sat Jul 07, 2007 2:55 pm

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

ctype_alnum()... no regex needed.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
bos45430
Forum Newbie
Posts: 12
Joined: Sun Jul 08, 2007 11:11 am

Post by bos45430 »

perfect! thanks!

john
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

If spaces are ok use this instead.

Code: Select all

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