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.
3 Answers
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']);
}
}
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-FizNov 5, 2011 at 18:58
-
-
3Since 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.– apaderno ♦Nov 5, 2011 at 19:51
-
-
The link is now: admin/config/people/user-restrictions/add to do this Feb 5, 2016 at 21:33
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.– CheesoMay 24, 2016 at 17:30