Laravel Validation Rule for Email

I have no idea how to make validation rules for an email field without accepting email like gmail, yahoo etc. Can anyone guide me on this topic. thanks in advance

+6
source share
3 answers

Put the list of email providers in the configuration file, for example, in config/email.php:

'banned_email_providers' => '@gmail\.com|@yahoo\.com'

Then use the regexvalidation rule using this list:

'email' => ['regex:/^((?!' . config('email.banned_email_providers') . ').)*$/'],
+4
source

You can save the name of the mail sites and check the email with its values

$free = [
   'google.com',
   'yahoo.com'
];

$mailSite = substr($email, strpos($email, '@') + 1);

if (in_array($mailSite, $free) {
    exit('free email');
}
0
source

\config\mail.php

'banned_servers' => ['yahoo.com', 'gmail.com']

    $rules = [
        'email' => array('regex:/@(?!' . implode('|', array_map(function ($item){return str_replace('.', '\.', $item);}, config('mail.banned_servers'))) . ')/')
    ];

    $messages = ['email.regex' => 'Email accounts from '. implode(', ', config('mail.banned_servers')) . ' are not allowed'];

    $this->validate(Request::instance(), $rules, $messages);
0

All Articles