Laravel: 电子邮件验证

Laravel: validation for email

我不知道如何为电子邮件字段制定验证规则。 当有人注册时,我想接受不是来自服务器的邮件 ID,比如 '@myemail.com、@yahoo.com、@outlook.com'。如果给出此邮件地址,它会说您的邮件 ID 无效。我该怎么办??

请帮帮我,这是我的 ResigterController.php

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
        'mobile_no' => ['required', 'string', 'min:10'],
        'company' => ['required', 'string', 'max:255'],
        'username' => ['required', 'string', 'max:255', 'unique:users'],

    ]);
}

我的配置目录中没有 email.php。

您可以在此基础上添加正则表达式验证。

$rules = [
    'name' => ['required', 'string', 'max:255'],
    'email' => [
        'required',
        'string',
        'email',
        'max:255',
        'unique:users',
        'regex:/^\w+[-\.\w]*@(?!(?:outlook|myemail|yahoo)\.com$)\w+[-\.\w]*?\.\w{2,4}$/'
    ],
    'password' => ['required', 'string', 'min:8', 'confirmed'],
    'mobile_no' => ['required', 'string', 'min:10'],
    'company' => ['required', 'string', 'max:255'],
    'username' => ['required', 'string', 'max:255', 'unique:users'],
];
// Add a custom message for regex validation on email field.
$messages = [
    'email.regex' => 'We do not accept mails with this domain.'
];

Validator::make($data, $rules, $messages);

我改编了 a reply to another question. The regex should work according to regex101.com 的正则表达式。

另一种选择是制定您自己的验证规则。请参阅 documentation