How to Validate Email in PHP?

Validate Email Address in PHP using preg_match – Simple with Code

<?php 
// Techyadda.in PHP program to validate email 
  
// Function to validate email in PHP 
function email_validator($email) { 
    return (!preg_match( 
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $str)) 
        ? FALSE : TRUE; 
} 
  
// Here we are calling function
if(!email_validator("techyadda19@gmail.com")) { 
    echo "Email address not valid."; 
} 
else { 
    echo "Valid email address."; 
} 
  
?> 

So using the above function you can easily validate email in PHP. People must validate email on the server side (PHP) as one can bypass javascript validation easily.

Please share your suggestion and feedback in the comments. And please also suggest more articles related to Website Development. Thanks for visiting!!!

Leave a Comment