Laravel:电子邮件的自定义验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41933019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:13:41  来源:igfitidea点击:

Laravel:custom validation for email

laravelemaillaravel-5.3

提问by incorporeal

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??

当有人注册时,我想只接受来自一台服务器的邮件 ID 说“@myemail.com”。如果给出任何其他邮件地址,它会说您的邮件 ID 无效。我该怎么办??

   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

它是我的注册控制器的验证器

回答by Sebastian

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.

注意:使用正则表达式模式时,可能需要在数组中指定规则而不是使用管道分隔符,尤其是当正则表达式包含管道字符时。

回答by Miloud BAKTETE

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

由于添加了 laravel 5.8.17ends_with验证规则,如下所示:

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

回答by Nikunj Kabariya

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);}

回答by Nikunj Kabariya

You can do some changes in boot method of app service provider also.

您也可以对应用服务提供商的启动方法进行一些更改。

Create custom validation rule - click here

创建自定义验证规则 - 单击此处