[solved]how to check for banned characters

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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

[solved]how to check for banned characters

Post by vigge89 »

For my registration page, i don't want the user to be able to use 'bad' characters, ie, only [a-z], [A-Z], [0-9], [_], [-] should be allowed.
But how could i check if there are any 'banned' characters in the requested username?
Last edited by vigge89 on Sat Jan 31, 2004 3:59 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You could use regular expressions. Someone who knows about them should be able to pop one out really quick. I am not one of those people so I would use PHP's string functions. Break the username down character by character and compare that character against legal characters. Dirty I know, but it works.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Native ctype fns are another nice option - not always enabled though.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Maybe something like:

Code: Select all

if(preg_match('/([^a-z0-9_-])/i', $username)){
  echo 'Invalid Name';
} else {
  echo 'Valid Name';
}
..but my regex is a but rusty so i'm not 100% that catches everything, but tests seem to go ok :o
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

i'll try that out :D
at first, if I wouldn't have created this topic, I would go for the big boss,
str_replace :P
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

So looking for a preg_match with this will do: /[^\w]/

The \w matches any word character, a-zA-Z0-9_
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

great, it works, but why does it return false if the given string matches the pattern?
thats kinda strange :?
anyway, Thanks!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Thats because of the ^ in front.

Normally, preg_match on /[a]/ would return true of there was an 'a' in the string
the [^a] returns true if there is a character different from 'a' in it.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ah, ok.
I know nothing about RegExp, but could someone give me a tutorial or anything about it?
it seems to be so hard :?
Post Reply