Page 1 of 1

need a regular expression for check numbers&letters&_ only

Posted: Tue Apr 17, 2012 1:36 am
by mostafa272
Hi

I check username in my login page with following code that it allow to have a username with numbers and letters and _ only!

Code: Select all

$user=preg_replace("([^a-z0-9_]*)",'',$user);
now,I want to forbid users to have a username with other characters in my register page and I want to check it with preg_match function,but I don't know what regular expression is suitable for it!please help me!

thanks

Re: need a regular expression for check numbers&letters&_ on

Posted: Tue Apr 17, 2012 7:02 am
by requinix

Code: Select all

/[^a-z0-9_]/
Use that with preg_match. If it matches then the username is invalid.

Re: need a regular expression for check numbers&letters&_ on

Posted: Tue Apr 17, 2012 10:25 am
by mostafa272
it is not that expression i need it.I want an expression that find every character in a string except small letters,numbers,and _.
because username must has a small letters,numbers,_. and other things will be replaced with above code.
users can select a username with small letters,numbers,_.i want to find other characters in usernames and send error message to users.

Re: need a regular expression for check numbers&letters&_ on

Posted: Tue Apr 17, 2012 11:54 am
by requinix
There was a typo: missed out on the ^.

So you actually want to know every single invalid character in the username? Same expression still, just give it to preg_match_all().
Or if you only care about knowing whether the username is invalid at all - whether it contains anything that's not a letter, number, or underscore - then it's still that expression.