Validate username as alphanumeric with underscores

Viewed 71419

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?

6 Answers
Related