我不知道如何制定验证规则,以使电子邮件字段不接受像Gmail、Yahoo等免费账户的电子邮件。有人可以在这个问题上给我指导吗?提前致谢。
config/email.php
文件中:'banned_email_providers' => '@gmail\.com|@yahoo\.com'
然后使用regex
验证规则,使用以下列表:
'email' => ['regex:/^((?!' . config('email.banned_email_providers') . ').)*$/'],
$free = [
'google.com',
'yahoo.com'
];
$mailSite = substr($email, strpos($email, '@') + 1);
if (in_array($mailSite, $free) {
exit('free email');
}
'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);