How to prevent users from being registered when their username contains a space? I'm trying to prevent a user from being saved with Rules when he/she tries to use a username that contains a whitespace. I've already read How can I disallow spaces in usernames? and used the User restrictions module to achieve this, but I'd like to use Rules to do some other stuff (e.g. checking that the username doesn't have more than 20 characters).

So, what I'd like to do is showing an error message on the page and keep all the filled in information in the form without saving it. Is it possible to do this?

This is the Rule I'm currently using:


Event: Before saving a user account.
Conditions: NOT Execute custom PHP code. &lt;?php preg_match('/\s/',$account-&gt;name) == 1; ?&gt;
Actions: Show a message on the site.
@Jeroen,
Then I would suggest to do just one custom form alter to the user registration form and add a validation function that will validate each case for you.

/**
 * Implements hook_form_FORM_ID_alter().
 */
// Alter the registration form
function CUSTOM_MODULE_form_user_register_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  // Add your own function to the array of validation callbacks
  $form['#validate'][] = 'CUSTOM_MODULE_user_validation';
}

/**
 * Custom function to validate the user creations.
 */
function CUSTOM_MODULE_user_validation(&amp;$form, &amp;$form_state) {
  $name = $form_state['values']['name'];
  if (preg_match('/\s/',$name)) {
     form_set_error('name', t('Name should not contain white spaces.'));
  }
}


Its easier to use and doesn't invoke rules API every time. I also think this is much effective for performance.