Validate email domain in Laravel request

16,700

Solution 1

You could use a regex pattern for this. Append this to your email validation:

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|regex:/(.*)@myemail\.com/i|unique:users',
        'password' => 'required|min:6|confirmed',
        /*'usertype' => 'required',*/
     ]);
}

EDIT
With mutiple domains you have to use an array with your validations, because of the pipe between the two mail domains:

'email' => ['required', 'max:255', 'email', 'regex:/(.*)@(mrbglobalbd|millwardbrown)\.com/i', 'unique:users'],

Note: When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.

Solution 2

since laravel 5.8.17 ends_with validation rule was added, which looks like this:

$rules = [
    'email' => 'required|ends_with:laravel.com,jasonmccreary.me,gmail.com',
];

Solution 3

Please do the following change in your email validation line. You can expand the email validation in your validator rule like:

protected function validator(array $data){
  $messages = array('email.regex' => 'Your email id is not valid.');

  return Validator::make($data, [
    'name' => 'required|max:255',
    'email' => 'required|email|max:255|unique:users|regex:/(.*)\.myemail\.com$/i',
    'password' => 'required|min:6|confirmed',
    /*'usertype' => 'required',*/
 ], $messages);}
Share:
16,700
incorporeal
Author by

incorporeal

Curious about everything

Updated on July 09, 2022

Comments

  • incorporeal
    incorporeal almost 2 years

    I want to accept email from just one server when someone is registering, for example "@myemail.com". If any other email address is given it will say your email is not valid.

    Below is the validator of my registration controller. What should I do?

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
            /*'usertype' => 'required',*/
        ]);
    }
    
  • incorporeal
    incorporeal over 7 years
    what if i want to verify for 2 mailserver type id's myemail and myemail2 ?? where should i add that??
  • Sebastian
    Sebastian over 7 years
    You could update the pattern and include multiple domains in brakets. I've updated my answer.
  • incorporeal
    incorporeal over 7 years
    it shows error ErrorException in Validator.php line 1798: preg_match(): No ending delimiter '/' found
  • Sebastian
    Sebastian over 7 years
    @incorporeal please show your code 'email' => '...
  • incorporeal
    incorporeal over 7 years
    here it is 'email' => 'required|email|max:255|regex:/(.*)@(mrbglobalbd|millwardbro‌​wn)\.com/i|unique:us‌​ers'
  • Sebastian
    Sebastian over 7 years
    @incorporeal sorry, my bad. If you use a pipe in the regex you have to use an array instead.
  • miken32
    miken32 over 2 years
    I'd suggest ends_with:@laravel.com... to be safe, and also keep the email rule in place.