I suddenly realized that username can be any character after seeing someone just made his or her account in Japanese characters. I don't really have a problem with that but just would like to let them register only with alphabetical characters to keep the things tidy. Is there anyway to do this?

link|improve this question

feedback

3 Answers

up vote 4 down vote accepted

Check out the Custom Username Validation module, it seems like it should do exactly what you like.

If not, you can set up "Access Rules" in the administration pages. Set up multiple access rules to deny usernames that contain undesired symbols. Access Rules Docs. Unfortunately that would take a lot of time to deny all non-alphabetical characters.

Finally, you could also create a custom module and use hook_form_alter() (or hook_form_FORM_ID_alter()) to change the validation on the username field. Here's a related question on SO: Hook into Drupal registration and validate user info against business logic

link|improve this answer
Thanks I ended up using Custom Username Validation module with some regular expression. it's perfect and easy. – chinita7 Feb 22 at 15:39
feedback

Using hook_form_alter

You can do like this,

function hook_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'user_register'){
    $form['#validate'][] = 'user_name_validate';
  }
}

function user_name_validate($form, &$form_state) {
  $name = $form_state['values']['name'];
  if(preg_match('/[^A-Za-z]/', $name)) {
    form_set_error('name', 'username contains non alphapetic');
  }
}
link|improve this answer
feedback

In addition to the module or hook_form_alter() you can also use the username originality AJAX check module to show a message to your user before they submit the form.

I was able to modify the code in that module to do my custom validation - I don't like special characters in usernames either ;-)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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