amit rawat
amit rawat

Reputation: 783

Validation of an email address in Laravel

I have created an edit form in my Laravel application form which accepts an email address. I have used Laravel validation on the server-side. It is validating the email address correctly when I pass a clearly invalid value like 'xxxxxx'.

But the problem is when I send an email address with only a top level domain and no dot like 'xxxxxx@yyyyyy', it accepts it as a valid email address.

How can I validate the email address to ensure it's using a proper domain?

Upvotes: 40

Views: 152296

Answers (6)

Alex Aaqil
Alex Aaqil

Reputation: 227

In Laravel 10:

Try doing: 'email' => ['required', 'email:rfc,dns'],

Laravel uses the rfc5322 standard for email validation, which is based on RFC 5322. This standard defines the syntax for email addresses and is widely followed in practice.

The dns option checks whether the domain of the email address has valid DNS (Domain Name System) records. This helps verify that the email address corresponds to an existing and properly configured domain on the internet.

Upvotes: 7

Theakash
Theakash

Reputation: 203

Laravel email validation and unique email insert database use for code:

'email_address' => 'required|email|unique:customers,email_address'

Upvotes: 3

user10533192
user10533192

Reputation:

It is not a Laravel issue. That is technically a valid email address.

Notice that if you tell the browser to validate the email address, it will also pass.

But you can use package EmailValidator for validating email addresses.

At first, also check these: https://laravel.com/docs/6.x/validation#rule-email

Or,

use the checkdnsrr function.

<?php

    $email = '[email protected]';
    list($username, $domain) = explode('@', $email);

    if (checkdnsrr($domain, 'MX')) {
      echo "verified";
    }
    else {
      echo "failed";
    }

Upvotes: 5

albus_severus
albus_severus

Reputation: 3712

Try this:

$this->validate($request, [            
  'email' => 'required|regex:/(.+)@(.+)\.(.+)/i',
]);

Upvotes: 11

jasme
jasme

Reputation: 739

With Laravel 7: you can use

'email' => 'email:rfc,dns'

Upvotes: 73

Anil Stha
Anil Stha

Reputation: 581

You can simply do:

$validator = Validator::make($request->all(), [
        'Email'=>'required|email'
    ]);

Upvotes: 13

Related Questions