Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
1  
Use the same solution as in your last question and change the regex. –  toscho Nov 27 '13 at 17:34

2 Answers 2

Use this regular expression

/^[a-z0-9]{3,16}$/

(3,16) used for limit the characters

share|improve this answer
2  
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
up vote 1 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

Your Answer

 
discard

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.