5

I hate spaces in usernames, a user can easily add two or more spaces without realizing it. I need to have more control over the username format than Drupal allows by default.

9

I recently wrote a custom module to disallow spaces and other characters in usernames. Feel free to comment on the code. I also modified username_check to do the same validations in the ajax callback.

//implement hook_form_FORM_ID_alter
function custom_form_user_register_form_alter(&$form, $form_state, $form_id) {
  //define validation function
  $form['#validate'][] = 'my_user_register_validate';
  //overwrite the username help text
  $form['account']['name']['#description'] = 'Username has to be between 5-15 characters long and can only contain letters and numbers';
}

function my_user_register_validate($form, &$form_state) {

  $output['error'] = FALSE;
  $username = $form_state['input']['name'];
  $password = $form_state['input']['pass']['pass1'];

  if (strlen($username) < 5 || strlen($username) > 15) {
    $output['error'] = TRUE;
    $output['msg'] = t('The username has to be between 5 and 15 characters long. ');
  }
  if (strpos($username,' ') > 0 || strpos($username,'.') > 0 || strpos($username,'-') > 0 || strpos($username,'_') > 0 || strpos($username, "'") !== FALSE ){
    $output['error'] = TRUE;
    $output['msg'] = t('Username cannot contain spaces . - _ ' . chr(39)  );
  }

//display username errors
  if ($output['error']){
    //drupal_set_message( $output['msg'], 'error');
    form_set_error('name', $output['msg']);
  }
}
  • Did you try this on Drupal 6 or 7? it doesn't seam to work on D7. I replaced the "custom_" part with "mymodulename_" – J-Fiz Nov 7 '11 at 19:34
  • D7. You might have to clear your caches. – uwe Nov 7 '11 at 21:26
  • 1
    Thank's, every once in a while I forget to do the simple things in Drupal. – J-Fiz Nov 7 '11 at 21:37
  • no problem, I do the same ;-) – uwe Nov 7 '11 at 22:57
  • 1
    Great this worked like a charm! – SsouLlesS Sep 15 '14 at 5:23
8

User restrictions contrib module is a replacement for the 'access rules' functionality which was removed from Drupal 7.

You can make an access rule to prevent this with username creation.

Go to administer » access control, tabs account rules » add rules (admin/access/rules/add) and add the rules

% %
% %

(The last line has an ASCII 255 character)

Similar rules for dashes are also possible.

This is based on conversation at drupal.org

  • In Drupal 7 "admin/access/rules/add" takes me to the administration page the functionality you speak of seams to be missing from D7. Is that a module that I have to install? – J-Fiz Nov 5 '11 at 18:58
  • I updated my answer above. You need a contrib module in D7 to do this. – Refineo Nov 5 '11 at 19:13
  • 3
    Since I am the developer of the User restrictions module (and became the maintainer only recently), I will add a note. I am rewriting the code, and the last available version still have some bugs. I will create a new official release between 7 days. Until then, don't install the module on a production site; before to use it for production, test it in a test site. – kiamlaluno Nov 5 '11 at 19:51
  • @kiamlaluno, thank you, I am looking forward for the official release. – Refineo Nov 5 '11 at 20:01
  • The link is now: admin/config/people/user-restrictions/add to do this – Lee Woodman Feb 5 '16 at 21:33
1

You can use a username validation in Drupal7 is easy to remove some type of characters and white spaces and you can add your own type of validation also , please check below function.

function my_module_my_form_validate($form, &$form_state) {

//birth year
  $year_of_birth = $form_state['values']['year_of_birth'];
  if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
    form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
  }

//username
  $username = $form_state['values']['uname'];   
  if (strpos($username,' ') > 0 || strpos($username,'.') > 0 || strpos($username,'-') > 0 || strpos($username,'_') > 0 || strpos($username, "'") !== FALSE ){
    form_set_error('uname','User name cannot contain spaces . - _ ' . chr(39)  );
  }  
}
  • What is implied by the name my_module_my_form_validate ? I think the question was specifically about adding validation to user information entered during registration. So .... shouldn't there not be a specific module name in there somewhere? This answer seems simple and clear, but there's not enough information here to make it useful. – Cheeso May 24 '16 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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