2

I want to validate domain when user enter invalid domain eg theemail@invaliddomainame.com

$createAccount_validator = Validator::make($request->all(), [

        'username'=>'required|min:6',
        'email'=>'required|email|unique:userTable)',

]);

Currently API of email validation is premium so I don't want to use.

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
user3151197
  • 347
  • 1
  • 3
  • 14
  • So, you want to exclude a list of email hosts? – Jerodev Feb 06 '17 at 13:31
  • @Jerodev I want to validate when user enter invalid domain which not exists. – user3151197 Feb 06 '17 at 13:45
  • 5
    @user3151197 That's quite complex. You'd have to do a WHOIS, a query for MX records, a query for A records, etc. Would take several seconds to run, most likely, and wouldn't be particularly reliable... and you'd still wind up with lots of valid domains but invalid emails still. If you need a real email, send a verification email to it with a link to click. – ceejayoz Feb 06 '17 at 13:46
  • @ceejayoz if laravel allow me I can validate domain get_headers. – user3151197 Feb 07 '17 at 04:40
  • @user3151197 While possible with a Laravel custom validator, that won't work. Plenty of emails come from valid domains that don't have a website on the domain. – ceejayoz Feb 07 '17 at 13:12
  • What does "validating" a domain even mean? There are way too many variables and as said the only way to know if an email is functional is to send an email to it. – miken32 Mar 19 '23 at 18:44

1 Answers1

0

Under ~app/Http/Requests/UserRequest.php you can use a regex for the email_domain field like

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        ...
        'email_domain' => [
            'required', 
            'regex:/^(?!\-)(?:(?:[a-zA-Z\d][a-zA-Z\d\-]{0,61})?[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/',
        ]
    ];
}

This pattern I'm using conforms with most of the rules defined in the specs. Notice that in this case we need to use / before and after.

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145