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. <?php preg_match('/\s/',$account->name) == 1; ?>
  • Actions: Show a message on the site.
  • Is it really necessary to use rules in this case? If you are open for custom codes then I can help you out. Commented Aug 2, 2014 at 16:47
  • @AneekMukhopadhyay: I always prefer Rules above code, because I can work faster with them, but custom coding is fine too. :)
    – Jeroen
    Commented Aug 2, 2014 at 16:52

2 Answers 2


@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(&$form, &$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(&$form, &$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.

  • I agree. It is a far more maintainable solution. Commented Aug 2, 2014 at 17:21
  • Thanks for your help. Is this code already supposed to work? I've used it in my custom module, but there's no error thrown when a new user is registrated with a space in it's name. I'd also like to prevent this from happening when an existing user edits his/her account.
    – Jeroen
    Commented Aug 3, 2014 at 13:54
  • Hello, this should work. Have you cleared the Drupal cache after adding this bit of code? Commented Aug 3, 2014 at 15:32
  • I've rediscovered this old question of mine and found two small errors in your code. I've edited your answer to correct them and everything works fine now. Sorry for letting you wait for over a year to gain your well deserved points. I'll give you an extra upvote. ;-)
    – Jeroen
    Commented Sep 12, 2015 at 20:25

I don't think using Rules for form validation is the right way to go. It is too messy and complicated.

I will rather do a form alter on the registration form and add my own custom validation handler.

Or try using Field Validation module. I have never used it on user fields before, but I don't see why it won't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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