Page 1 of 1

Checking a Username for valid characters

Posted: Wed Nov 30, 2005 12:01 am
by cent
Hi All,

Trying to figure out something that I have no real knowledge of... preg_match. I search around the boards here and other PHP programming sites and put together the closest thing I could find that suits my needs. BUT... it doesn't work right.

Basically, the username passed to the function should only contain A-Z, a-z, 0-9, the underscore "_" character and no spaces. Anything else should return false.

Code: Select all

function checkUsername($uname)
{
	return preg_match("/[A-z]+[0-9]+[_]/",$uname);
}

Am I missing something here or did I format it incorrectly? As I mentioned, preg_matches and ereg calls are new to me.

Thanks in advance,

Best,
Cent

Posted: Wed Nov 30, 2005 1:12 am
by allanonschick

Code: Select all

function checkUsername($uname) 
{ 
    return preg_match('/^[A-Za-z0-9_]+$/', $uname);
Can you try this instead?

Posted: Wed Nov 30, 2005 11:38 pm
by cent
That did the job... thanks for the help.

Best,
Cent.

Posted: Thu Dec 01, 2005 10:09 am
by pickle
I'm by no means a regex guru, but I think this pattern would work too:

Code: Select all

/^(\w)*$/
\w is a meta character meaning all word characters (a-z, A-Z, 0-9, and _).