<p>One way of doing this is to write a custom plugin that intercepts the <a href="https://craftcms.com/docs/plugins/events-reference#users-onBeforeSaveUser" rel="nofollow noreferrer"><code>users.onBeforeSaveUser</code> event</a>.</p>

<p>You would check that the submitted email address matched your desired pattern and set <code>$event-&gt;performAction = false</code> if the criteria wasn't met. This would prevent the user from being saved.</p>

<p>So the <code>init()</code> method of your plugin might look something like this:</p>

<pre><code>    public function init()
    {
        craft()-&gt;on('users.beforeSaveUser', function(Event $event) {

            // Retrieve the userModel from the event
            $user = $event-&gt;params['user'];
            $isNewUser = $event-&gt;params['isNewUser'];

            // Check if this is a front end request and that we are dealing with a new user
            if (craft()-&gt;request-&gt;isSiteRequest() &amp;&amp; $isNewUser) {

                // Check for valid email address
                if (email address doesn't match required pattern) {
                    // Cancel user save
                    $event-&gt;preformAction = false;
                }
            }
        });
    }
</code></pre>

CC BY-SA 3.0
2017-01-25T17:24:28.377Z
This looks like a great solution, but I should have prefaced my question by saying I know zero PHP. Can you give detailed steps on how to implement this?
jasonetco
CC BY-SA 3.0
2017-01-25T23:42:17.570Z
Nevermind I figured it out! Thank you so much for your help!
jasonetco
CC BY-SA 3.0
2017-01-25T09:31:57.960Z
2017-01-25T09:31:57.960Z
Steve Rowling
<p>I want users to be able to signup to my Craft site, but only if they have a specific email domain. So:</p>

<pre><code>*@gmail.com
*@hotmail.com
</code></pre>

<p>Would be allowed, but anyone with a <em>yahoo.com</em> email, for example, would not be able to sign up. I can do client-side validation but is there anything I can do in Craft?</p>

<p>Thanks!</p>

<users><emails>
Only allow signups from people with @___.com emails
# Only allow signups from people with @___.com emails

(Asked by: username_0 on 2017-01-25)

I want users to be able to signup to my Craft site, but only if they have a specific email domain. So:
*@gmail.com
*@hotmail.com

Would be allowed, but anyone with a yahoo.com email, for example, would not be able to sign up. I can do client-side validation but is there anything I can do in Craft?
Thanks!

**Tags:** users, emails


---

## 1 answers

### Answer by username_1 on 2017-01-25. Score: 4 | Accepted answer

One way of doing this is to write a custom plugin that intercepts the users.onBeforeSaveUser event.
You would check that the submitted email address matched your desired pattern and set $event->performAction = false if the criteria wasn't met. This would prevent the user from being saved.
So the init() method of your plugin might look something like this:
    public function init()
    {
        craft()->on('users.beforeSaveUser', function(Event $event) {

            // Retrieve the userModel from the event
            $user = $event->params['user'];
            $isNewUser = $event->params['isNewUser'];

            // Check if this is a front end request and that we are dealing with a new user
            if (craft()->request->isSiteRequest() && $isNewUser) {

                // Check for valid email address
                if (email address doesn't match required pattern) {
                    // Cancel user save
                    $event->preformAction = false;
                }
            }
        });
    }

> **Comments:**
> **username_0**: This looks like a great solution, but I should have prefaced my question by saying I know zero PHP. Can you give detailed steps on how to implement this?  
> **username_0**: Nevermind I figured it out! Thank you so much for your help!  

---

