Laravel Email .(dot) Validation Example

Laravel 6 , PHP , Laravel , Laravel 7 , Laravel 8

Nicesnippets

358

23-10-2020


Laravel Email .(dot) Validation Example

Now let's see example of how to validate an eamil address in laravel and php. We will show email validation in laravel. we will learn how to validate an email with regex in laravel. if you want to check .(dot) from the email address exist or not. The problem is when I remove . (dot) from the email address, it accepts it as a invalid email address.

This article will give you how to validate .(dot) from email address in laravel and php. I will give three solution for .(dot) validation of email address in php laravel. one solution in laravel and two solution in php. So let's see the bellow solution.

Solution : In Laravel


/**

* Get the validation rules that apply to the request.

*

* @return array

*/

public function store(Request $request)

{

$request->validate([

'email' => 'regex:/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix'

]);

}

Solution 1 : In PHP

The function preg_match() checks the input matching with patterns using regular expressions.

<?php

function validEmail($str) {

return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;

}

if(!validEmail("abc@abccom")){

echo "Invalid email address.";

}

else{

echo "Valid email address.";

}

?>

Output

Invalid email address.

In the above example PHP preg_match() function has been used to search string for a pattern and PHP ternary operator has been used to return the true or false value based on the preg_match return.

Solution 2 : In PHP

In this solution we will use filter_var() method for email validation in php.

<?php

function checkemail($str) {

return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;

}

if(!checkemail("abc@abc.com")){

echo "Invalid email address.";

}

else{

echo "Valid email address.";

}

?>

Output

Valid email address.

It will help you....


Recommended Posts