1

In my blade file i have a html form for user in which input field email have type email. But i think it provide inconsistent email format validation. example it show abc@xyz and abc@xyz.com both are correct email address but in my controller, validator method return me invalid email format for abc@xyz. How can i resolve this issue???

In my blade file:

<input type="email" name="email" value="">

And in my controller:

$validator = Validator::make($request->all(), [
     'email'       => 'email|unique:table_name,col_name',
    ],[
     'email.unique' => 'email is already in used',
   ]);

if ($validator->fails()) {
    return redirect()->back()->withInput($request->all())->withErrors($validator);
  }
Ram Agrawal
  • 99
  • 4
  • 13
  • 1
    I guess `abc@xyz` isn't valid. https://en.wikipedia.org/wiki/Email_address –  Feb 21 '18 at 07:29
  • yes, abc@xyz isn't valid email address, but html input field type="email" accept it as valid email address format – Ram Agrawal Feb 21 '18 at 07:56
  • That's [ok (see email paragrapgh)](https://www.w3schools.com/html/html_form_input_types.asp). You see that `user@localserver` is till valid, according to wiki. You can add your own front-end validation with jQuery. –  Feb 21 '18 at 08:54
  • can you provide that jQuery code – Ram Agrawal Feb 21 '18 at 09:05

1 Answers1

1

Actually Laravel using PHP builtin function called filter_var (source code).

// Illuminate\Validation\Concerns\ValidateAttributes

/**
 * Validate that an attribute is a valid e-mail address.
 *
 * @param  string  $attribute
 * @param  mixed   $value
 * @return bool
 */
public function validateEmail($attribute, $value)
{
    return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
}

The rules implemented by PHP is quite different with the rules implemented by the browser. abc@xyz passed the browser validation rule because xyz is a valid hostname (you can read it here). But PHP implementing more strict rule that the email address should contains top level domain.

Dharma Saputra
  • 1,524
  • 12
  • 17