1

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!

4

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;
                }
            }
        });
    }
| improve this answer | |
  • 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 Jan 25 '17 at 17:24
  • Nevermind I figured it out! Thank you so much for your help! – jasonetco Jan 25 '17 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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