Validation: return only letters/numbers

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
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Validation: return only letters/numbers

Post by Locust »

Simply problem but I can't find exactly what I want.

I have an HTML form that puts values from the form into a file. For a few of these values I want it to only accept letters and numbers (no spaces) as well as no symbols. How do I do it?

Thanks :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

regular expressions. We have a board dedicated to just that, but before you ask for the solution, try to figure it out yourself by reading the stickies we've got in that board first.
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Post by Locust »

Well I figured my problem is too broad and simple - a search engine doesn't know what I'm looking for.

The search continues.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

ctype_alnum()
much better than a regular expression because you can't get it wrong.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

ole wrote:

Code: Select all

ctype_alnum()
much better than a regular expression because you can't get it wrong.
Yeah regex isn't need for something this simple. You may want to download a copy of the PHP manual and have a look at all the ctype functions.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

hang just realised that you might want to filter out all the non-alphanumeric characters rather than test for them. In which case you need to use this (this is a regular expression):

Code: Select all

public static function getAlphanumeric($value)
	{
		return preg_replace('/[^[:alnum:]]/', '', $value);
	}
mods: please not this code cannot be put in

Code: Select all

bb tag, it removes the :alnum: bit for some reason.[/b]
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

Yeah, I'm a big fan of filtering with ctype when possible.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

Word ole, I'd never seen the :alnum: what other types are there like that? What's that called in regex?
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Post by Locust »

Thanks for the help. ctype_alnum seems to work the way I want it. Now, however, in another field I want it to allow letters, numbers and spaces. Like ctype_alnum, I'd like to be able to test this rather than replacing values (regular expressions don't seem to test, they just replace) because I want to put this into an if() statement.

Code: Select all

if (ctype_alnum($_POST['field'])) {return true;}
else {return false;}
}
This works fine, now I want to be able to do the same but I want it to allow spaces.

Ty :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

regex functions can match (preg_match(), ereg()).
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

shoebappa wrote:Word ole, I'd never seen the :alnum: what other types are there like that? What's that called in regex?
Don't know, I only recently saw it myself when it was used in Zend_Filter of the Zend Framework
Post Reply