PHP 

This is an example how to validate email address in PHP. We need to validate email address in most of the applications now a days because in every application we need to have email sending functionality for various reasons like username reset, password reset, email reset etc.

Below function checks how to validate the email address in php.

function is_email_valid($email) {
    if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", trim($email))) {
        return TRUE;
    }
    return FALSE;
}

 

Usage examples

$email = "abc";
$valid = is_email_valid($email);
echo $valid ? "True" : "False";

Output: False

echo "\r\n";

$email = "abc@";
$valid = is_email_valid($email);
echo $valid ? "True" : "False";

Output: False

echo "\r\n";

$email = "abc@roytuts.com";
$valid = is_email_valid($email);
echo $valid ? "True" : "False";

Output: True

echo "\r\n";

$email = "abc@roytuts.com.com";
$valid = is_email_valid($email);
echo $valid ? "True" : "False";

Output: True

 

Thanks for your reading. Please do not forget to leave a comment.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *