Validate username as alphanumeric with underscores


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?

8/25/2009 8:14:13 PM

Accepted Answer

The actual matched characters of \w depend on the locale that is being used:

A "word" character is any letter or digit or the underscore character, that is, any character which can be part of a Perl "word". The definition of letters and digits is controlled by PCRE's character tables, and may vary if locale-specific matching is taking place. For example, in the "fr" (French) locale, some character codes greater than 128 are used for accented letters, and these are matched by \w.

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:

8/25/2009 8:26:33 PM

Here's a custom function to validate the string by using the PHP ctype_alnum in conjunction with an array of allowed chars:


$str = "";
function validate_username($str) {

  // each array entry is an special char allowed
  // besides the ones from ctype_alnum
  $allowed = array(".", "-", "_");

  if ( ctype_alnum( str_replace($allowed, '', $str ) ) ) {
    return $str;
  } else {
    $str = "Invalid Username";
    return $str;


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow