Filter input to only letters and 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
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Filter input to only letters and numbers

Post by tarja311 »

Hello all.

I have an input form that sends data to my database. This is fine, the problem is my extremely basic filtering. I am trying to make it so only letters ( a-z A-Z ) and numbers are allowed through. I think i am using preg_match incorrectly because it only checks if the first digit is valid or invalid. It does not validate the whole string.

example

valid : abc123
invalid : abc_#123

Code: Select all

if(!preg_match('/a-z A-Z 0-9 {1, 20}/', $input){
// exit ...
}else{
// continue ...
}
Any ideas?

Thanks

-- tarja
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Firstly, you forgot the parentheses (which I assume was from the way you wrote it to the board hopefully), but yeah:

Code: Select all

if(!preg_match('/[a-zA-Z0-9]{1, 20}/', $input))
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

typo'd.

Maybe i don't understand how preg_match works. With the code you provided, if i type in #, it says it's invalid, which is good. but if i type #a it says it's valid. I don't get it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

Thanks that is exactly what i'm looking for.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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

Post by Ollie Saunders »

The reason why the regex allowed #a is because it only have to find a match somewhere inside the string. For instance
Regex: /abc/
you should learn your abcs - match found

To prevent this you should use the start (^) and end ($) assertions. Which basically say there should be nothing before and nothing after, respectively.
Regex: /^abc$/
you should learn your abcs - no match found
abc - match found
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

Thanks for that info. noted. :)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Silly me. \b is an alternative right? That's what I use.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

\b is for word boundaries. Symbols and spaces are boundaries.. ;)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I always wondered why anyone would use ^$... \b is so much neater in my opinion. :o
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Everah wrote:ctype_alnum().
I've never noticed the ctype functions before. Crazy useful.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

ole wrote:The reason why the regex allowed #a is because it only have to find a match somewhere inside the string. For instance
Regex: /abc/
you should learn your abcs - match found

To prevent this you should use the start (^) and end ($) assertions. Which basically say there should be nothing before and nothing after, respectively.
Regex: /^abc$/
you should learn your abcs - no match found
abc - match found
Noted as well, thanks ole!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

onion2k wrote:
Everah wrote:ctype_alnum().
I've never noticed the ctype functions before. Crazy useful.
Dude, these are way useful. All of them. As soon as I found out about them I started using them. They are very nice. :D
Post Reply