Laravel:电子邮件的自定义验证 [英] Laravel:custom validation for email
问题描述
我想在有人注册时仅接受来自一台服务器的邮件ID,例如"@ myemail.com".如果提供了其他任何邮件地址,则会显示您的邮件ID无效.我该怎么办?
i want to accept mail id just from one server say '@myemail.com' when someone is registering. If any other mail address is given it will say your mail id is not valid. 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',*/
]);
}
它是我的注册控制者的验证者
it is the validator of my registration controller
推荐答案
您可以为此使用正则表达式模式.将此附加到您的电子邮件验证中:
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.
这篇关于Laravel:电子邮件的自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!