Answers.Body: <p>I don't know if there is anything natively in Craft to do this, but you could achieve it quite easily with a single file plugin:</p>
  
  <pre><code>public function init()
  {
      craft()-&gt;on('users.onBeforeSaveUser', function(Event $event) {
          // Only do anything if it is a front end submission
          if(craft()-&gt;request-&gt;isSiteRequest())
          {
              $username = craft()-&gt;request-&gt;getPost('username', null);
              if($this-&gt;isEmail($username))
              {
                  $event-&gt;params['user']-&gt;addErrors(array('username' =&gt; Craft::t('You cannot use an email')));
                  $event-&gt;performAction = false;
              }
          }
      });
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-04T21:40:56.473Z
Answers.Text: Thanks for this (apologies for the delay in responding, didn't get my notification!)
  I'll test this out shortly, thanks!
Answers.UserDisplayName: shorn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-21T21:10:19.090Z
Answers.Text: Hi, finally got round to trying this out. Getting an error though
  `...and its behaviors do not have a method or closure named "isEmail".` - Any idea?
Answers.UserDisplayName: shorn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-22T09:42:36.953Z
Answers.Text: Yes, sorry should have clarified, the `isEmail()` method is to imply you are using your own implementation of how you check whether the passed string is an email address and return a boolean
Answers.UserDisplayName: Alec Ritson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-22T09:45:04.660Z
Answers.Text: No probs. Kind of figured and actually sorted it last night. Now validates against an alpha numeric regex and seems to be working fine! Thanks!
Answers.UserDisplayName: shorn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-03T09:34:40.563Z
Answers.LastActivityDate: 2017-04-03T09:34:40.563Z
Answers.UserDisplayName: Alec Ritson
Answers.Body: <p>I ran into username validation because I had to migrate user names containing spaces (not allowed by default).</p>
  
  <p>I ended up creating a custom Username validator and let composer's autoloader use that one instead of craft's default. You can easily add your own validation rules there.</p>
  
  <pre><code>class UsernameValidator extends Validator
  {
    public function validateValue($value)
    {
      // your own validation rules here
  
      // (disabled) Don't allow whitespace in the username
      // if (preg_match('/\s+/', $value)) {
      //    return ['{attribute} cannot contain spaces.', []];
      // }
  
      return null;
    }
  }
  </code></pre>
  
  <p>Here is how to use the custom validator instead of the default one.
  Note that if Craft changes it's internals this might have to be adjusted.</p>
  
  <p><a href="https://io.24hoursmedia.com/tech-notes/allow-spaces-in-craft-cms-user-names" rel="nofollow noreferrer">https://io.24hoursmedia.com/tech-notes/allow-spaces-in-craft-cms-user-names</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-06T10:55:47.980Z
Answers.LastActivityDate: 2018-10-06T10:55:47.980Z
Answers.UserDisplayName: 24HOURSMEDIA
Body: <p>Just wondering if theres anyway to force some validation on the username?
  I have usernames being displayed locally, and therefore I'm trying to prevent email addresses being used. I have my client side validation set up with regex to prevent an email being used, but some users still seem to have been able to register with the email as there username (presumably they have JS disabled). </p>
  
  <p>It would be nice to enforce that validation server side.</p>
  
Tags: <users><validation>
Title: Validation on username?
ThreadText: # Validation on username?
  
  (Asked by: username_0 on 2017-04-03)
  
  Just wondering if theres anyway to force some validation on the username?
  I have usernames being displayed locally, and therefore I'm trying to prevent email addresses being used. I have my client side validation set up with regex to prevent an email being used, but some users still seem to have been able to register with the email as there username (presumably they have JS disabled).
  
  It would be nice to enforce that validation server side.
  
  **Tags:** users, validation
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-04-03. Score: 4 | Accepted answer
  
  I don't know if there is anything natively in Craft to do this, but you could achieve it quite easily with a single file plugin:
  
  ```
  public function init()
  {
      craft()->on('users.onBeforeSaveUser', function(Event $event) {
          // Only do anything if it is a front end submission
          if(craft()->request->isSiteRequest())
          {
              $username = craft()->request->getPost('username', null);
              if($this->isEmail($username))
              {
                  $event->params['user']->addErrors(array('username' => Craft::t('You cannot use an email')));
                  $event->performAction = false;
              }
          }
      });
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for this (apologies for the delay in responding, didn't get my notification!) I'll test this out shortly, thanks!  
  > **username_0**: Hi, finally got round to trying this out. Getting an error though `...and its behaviors do not have a method or closure named "isEmail".` - Any idea?  
  > **username_1**: Yes, sorry should have clarified, the `isEmail()` method is to imply you are using your own implementation of how you check whether the passed string is an email address and return a boolean  
  > **username_0**: No probs. Kind of figured and actually sorted it last night. Now validates against an alpha numeric regex and seems to be working fine! Thanks!  
  
  ---
  
  ### Answer by username_2 on 2018-10-06. Score: 1
  
  I ran into username validation because I had to migrate user names containing spaces (not allowed by default).
  
  I ended up creating a custom Username validator and let composer's autoloader use that one instead of craft's default. You can easily add your own validation rules there.
  
  ```
  class UsernameValidator extends Validator
  {
    public function validateValue($value)
    {
      // your own validation rules here
  
      // (disabled) Don't allow whitespace in the username
      // if (preg_match('/\s+/', $value)) {
      //    return ['{attribute} cannot contain spaces.', []];
      // }
  
      return null;
    }
  }
  
  ```
  
  Here is how to use the custom validator instead of the default one.
  Note that if Craft changes it's internals this might have to be adjusted.
  
  <https://io.username_2.com/tech-notes/allow-spaces-in-craft-cms-user-names>
  
  
  ---
  
  