php code to validate email address

An email address can be validated using php code. PHP web developers need to validate the email address for contact us, registration, profile,  login form page etc in website. Email address should be in correct form. So Form data submitted by user will send correctly in email. 

Validating email on the server side is a more secure way than validating it on the client side.

How to validate email address using php code?

We can use php function filter_var() with FILTER_VALIDATE_EMAIL to validate emails.

Please look ate the example code for validating email address.

<?php
function check_email($email)
{
    return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if(check_email("[email protected]")){
    echo "Your Email address is valid.";
}else{
    echo "Sorry, Your Email address is invalid!";
}
?>

Output :
Your Email address is valid.

Also We can also use below php function preg_match() for validating email address using the below regular expression pattern.

preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email);

Comments

Leave a Reply

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

64014