3

I have no idea how to make validation rules for the email field. I want to accept mail id which are not from server say '@myemail.com, @yahoo.com, @outlook.com' when someone is registering. If this mail address is given it will say your mail id is not valid. what should I do??

please help me, here is my 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'],

    ]);
}

controller.php

config.php

and I don't have email.php in my config directory.

Alok Mishra
  • 694
  • 5
  • 20

3 Answers3

1

You could add a regex validation on top of that.

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

I adapted the regex from a reply to another question. The regex should work according to regex101.com.

Another option is to make your own validation rule. Refer to the documentation for that.

IGP
  • 14,160
  • 4
  • 26
  • 43
  • `\w{2,4}` for TLD is wrong, it accept `1_b` but not `museum`. Please, have a look at these sites: [TLD list](https://www.iana.org/domains/root/db); [valid/invalid addresses](https://en.wikipedia.org/wiki/Email_address#Examples); [regex for RFC822 email address](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) – Toto Nov 22 '19 at 15:11
  • @Toto but it is working and sorry, I didn't get you – Alok Mishra Nov 22 '19 at 15:17
  • @Toto full disclosure: I do not know how or why this works. I just adapted a regex from another answer that happened to do what OP was looking for. – IGP Nov 22 '19 at 15:21
1
'email' => 'required|email|max:255|unique:users',
endo.anaconda
  • 2,449
  • 4
  • 29
  • 55
  • This is the exact same as the code that's in the question, except it's formatted differently. The question is about ensuring that emails with certain domains don't pass validation, which this doesn't do. – Laurel Aug 09 '22 at 11:41
0

Better to use config file seprated for example config\settings.php

<?php
    return [
      'restricted_email_domains'=>[
         'myemail.com', 'yahoo.com', 'outlook.com',
      ],
    ];

so, your code would be

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users',
                function ($attribute, $value, $fail) {
                   if (in_array($value, config('settings.restricted_email_domains')))
                   {
                       $fail('The '.$attribute.' is invalid.');
                   }
                },
            ],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
        'mobile_no' => ['required', 'string', 'min:10'],
        'company' => ['required', 'string', 'max:255'],
        'username' => ['required', 'string', 'max:255', 'unique:users'],

    ]);
}

Reference using-closures

meYnot
  • 343
  • 2
  • 6
  • 13