JohnA
JohnA

Reputation: 1913

Regex match complex conditions for username

I need to check if string has anything else than what specified below

everything else should not be in this string have have

i am stuck on this:

preg_match('/^[\w]{4,35}$/i', $username)

Upvotes: 2

Views: 231

Answers (2)

FailedDev
FailedDev

Reputation: 26930

preg_match('/^[\w]{4,35}$/i', $username)

OK lets see what this doesn't work :

Match the beginning of the string, followed by [a-zA-Z0-9_] at least 4 times with a maximum of 35 times. This is quite different from your requirements.

Instead what you should use :

/^[a-zA-Z][-,.\w]{3,34}$/

The case sensitivity i modifier is not needed. Also I don't think this is exactly what you want. Usually you would need to specify a minimum length which you don't. This can match "a" for example (not a good username)

Upvotes: 2

Antti Rytsölä
Antti Rytsölä

Reputation: 1545

if( !preg_match('/^[a-zA-Z][a-zA-Z0-9_,.-]*$/', $username ))
    echo "failed\n";

Upvotes: 7

Related Questions