5

I want to validate email. Suppose if a customer give an email address like "demo@gmail" it gives an validation error. Email should be "demo@gmail.com".What is the code to do this validation.I do it but not working properly.My code in below:

$request->validate([
   'email'=>'required|email',
]);
Foisal Hossain
  • 129
  • 1
  • 8

3 Answers3

12

The comments under the question are correct, BUT...

If you are looking for email validator with domain at the end, you can use this filter:

'email' => 'email:filter'

It uses the filter_var() method to check if the email is valid. Refer to https://laravel.com/docs/6.x/validation#rule-email for other email validation types.

Linas Butenas
  • 161
  • 1
  • 7
1

You can add the following to the email validation: email:rfc,dns. According to the docs, the default is rfc, which accepts emails without domain, so the dns rule is adding the domain check.

With the example it looks like this:

$request->validate([
    'email'=>'required|email:rfc,dns',
]);
Annity
  • 96
  • 1
  • 10
0

use this:

$rules = [
  'email' => 'required|email'
 ];
$validator = Validator::make($request->all(), $rules);
        if (!$validator->fails()) {
}
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
  • 1
    if a client does not give .com (at the end of email id) he/she will face an validation error.I want this validation also but can't get it – Foisal Hossain Oct 09 '19 at 10:18