On my registration page I need to validate the usernames as alphanumeric only, but also with optional underscores. I’ve come up with this:
function validate_alphanumeric_underscore($str)
{
return preg_match('/^\w+$/',$str);
}
Which seems to work okay, but I’m not a regex expert! Does anyone spot any problem?
The actual matched characters of
\wdepend on the locale that is being used:So you should better explicitly specify what characters you want to allow:
This allows just alphanumeric characters and the underscore.
And if you want to allow underscore only as concatenation character and want to force that the username must start with a alphabet character: