Originally Posted by
Zachafer
Personally, I'm a fan of allowing underscores (or spaces) in the username.
Also, you could use something like this for your username regex (specifies length 3-24):
PHP Code:
$rgx = '/^([a-z][a-z0-9]{2,23})$/i';
My original proposal was multiple hyphens that can't be in sequential order. But that seems like a bit much so possibly just a single hyphen.
PHP Code:
function validateUsername($test)
{
$rgx = '/^([a-z][a-z0-9-]{2,23})$/i';
return preg_match($rgx, $test) && strpos($test, '--') === false;
}
Or the all in one regex (wow)
- Case insensitive
- Username length 3-24
- Username starts with a-z
- Username can contain hyphens but cannot contain repeating hyphens
- Username allows character [a-z0-9-]
PHP Code:
$rgx = '/^[a-z](?:[a-zA-Z0-9]|(-)(?!\1)){2,23}$/i';