WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I limit the username field upon registration to A-Z and 0-9 for single site? So no spaces, hyphens, underscores, dots etc.

share|improve this question
Use the same solution as in your last question and change the regex. – toscho Nov 27 '13 at 17:34
up vote 2 down vote accepted

Using the regex posted by Moaz (and adding capitals), we will need to hook into the registration_errors filter:

// Restrict username registration to alphanumerics
add_filter('registration_errors', 'limit_username_alphanumerics', 10, 3);
function limit_username_alphanumerics ($errors, $name) {

 if ( ! preg_match('/^[A-Za-z0-9]{3,16}$/', $name) ){
    $errors->add( 'user_name', __('<strong>ERROR</strong>: Username can only contain alphanumerics (A-Z 0-9)','CCooper') );
 return $errors;
share|improve this answer

Use this regular expression


(3,16) used for limit the characters

share|improve this answer
Out of the box, WordPress enforces a 3-character minimum on usernames. Your {0,16} should probably be {3,16} to reflect this reality. (I had to add a filter to allow a 2-character username.) – Pat J Nov 27 '13 at 17:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.