I created a module but I'm looking to have this module to intercept a user log in. I want to prevent any users with "test" in their login name from using the site. Is there a way to do this?

3 Answers 3


Custom username validation would be an option

allows to define a validation pattern for the username on registration. it's also possible to customize the error message on validation failure.

  • I don't think it answers OP's concern. He wanted to prevent them from login, not registration. On many sites I have had users that was forbidden to login unless something got postal, like accounts for hosting company to let them test what's wrong. We simply inactivated them when not needed, but I can see how this is different need than just removing them altogether
    – Mołot
    Aug 19, 2013 at 14:15
  • @Mołot If it doesn't handle user login the OP can extract the code and apply it themselves. Or at least bother to reply to the answer within a week. I'm not writing a full tutorial for every low quality question that gets asked. People need to put effort in themselves, not rely on the few of us that bother to answer questions. You're free to downvote how you wish, of course, but I don't consider this to be a poor quality/un-useful answer (otherwise I'd just delete it). For the vague, broad question, it's a totally appropriate vague, broad answer. At least that's the story I'm sticking with ;)
    – Clive
    Aug 19, 2013 at 14:22

Hope this will help you,

In Drupal 7 you can achieve this using Clientside Validation module.

  • Clientside Validation : Adds clientside validation for all forms and webforms using jquery.validate

Note : Once module is enabled go through README.TXT for documentation.



It is pretty straight forward to answer this question using Rules. Here is a rule (in Rules import format) that should answer your question:

{ "rules_redirect_to_logout_for_test_users" : {
    "LABEL" : "Redirect to logout for test users",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_login" : [] },
    "IF" : [
      { "text_matches" : {
          "text" : [ "account:name" ],
          "match" : "\\btest\\b",
          "operation" : "regex"
    "DO" : [
      { "drupal_message" : {
          "message" : "User names are not allowed to contain TEST ...",
          "type" : "error"
      { "redirect" : { "url" : "user\/logout" } }

The rule will be triggered (Rules Condition) right after login was completed. For any user with a username containing "test", it will perform these Rules Actions (in the order specified):

  1. Set an appropriate error message.
  2. Redirect to the page user/logout (= force the user to be logged out again).


  • By default the rule is disabled (so don't forget to enable it also).
  • The above approach limits such user to only visiting anything that can be accessed by anonymous (you can't prevent a user with a name containing "test" to visit the site anonymous).

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.