<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>

CC BY-SA 3.0
2017-04-04T21:40:56.473Z
Thanks for this (apologies for the delay in responding, didn't get my notification!)
I'll test this out shortly, thanks!
shorn
CC BY-SA 3.0
2017-04-21T21:10:19.090Z
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?
shorn
CC BY-SA 3.0
2017-04-22T09:42:36.953Z
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
Alec Ritson
CC BY-SA 3.0
2017-04-22T09:45:04.660Z
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!
shorn
CC BY-SA 3.0
2017-04-03T09:34:40.563Z
2017-04-03T09:34:40.563Z
Alec Ritson
<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>

CC BY-SA 4.0
2018-10-06T10:55:47.980Z
2018-10-06T10:55:47.980Z
24HOURSMEDIA
<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>

<users><validation>
Validation on username?
# 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.24hoursmedia.com/tech-notes/allow-spaces-in-craft-cms-user-names


---

