How to Validate Email Address in PHP

How to Validate Email Address in PHP

Demo Download

Today in this tutorial, I will show you how to validate email address in PHP. Email address validation is one of the most common task when developing a web application.

As you know that email plays a key role in the world of web application, you will need to provide your email address to register on any website.

They all perform email validation before creating your account, if you type the incorrect email so you will get the error on the screen.

There are various ways to validate email address format in PHP. For example, you can use a regular expression or you can also use a PHP build in filter_var() function.

I will use the filter_var() function method and I will pass the FILTER_VALIDATE_EMAIL filter to this function to validate correct email address format.

Previously, I have shared a tutorial on how to validate email address in JavaScript. It is good practice that you validate your email address from both client side and server side.

PHP Email Address Validation

Here is the code for email validation using PHP, just create an index.php file and paste the following HTML form & message code in it.

HTML Form & Message Code:

    echo "<div class='alert alert-danger'><ol>".$error."</ol></div>";
    echo "<div class='alert alert-success'><ol>".$success."</ol></div>";
<form action="" name="myform" method="post">
    <label for="email"><strong>Enter Your Email</strong></label><br>
    <input type="email" name="email" id="email" placeholder="[email protected]" autofocus/><br>
    <input type="submit" value="Submit">

I am getting input of email address using this HTML form. When user press the submit button, the form will be submitted then I can validate email address input field data.

I am also displaying the error and success message to the user, but this message will be visible to user after form submission.

PHP Code:

Put this PHP code in the top of index.php file so that we can validate email address input field data using PHP.

$error = "";
$success = "";
    $email = $_POST['email'];
        $error .= "<li>Email address is a mandatory field.</li>";
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error .= "<li>Invalid email address format!</li>";
            $row = explode("@", $email);
            $domain = $row[1];
            if(!checkdnsrr($domain,"MX")) {
                $error .= "<li>Email address domain does not exist!</li>";
                $success = "<li>Email address is in valid format.</li>";


In the above PHP code, I am checking that when user submit the form first check that email address is not empty.

If the email address is not empty then validate the email address format using our filter_var($email, FILTER_VALIDATE_EMAIL) and passing our email variable and FILTER_VALIDATE_EMAIL filter as arguments.

It will return true if the email address is valid, otherwise it will return false.

Additionally, I am also using checkdnsrr() to check the domain address in email is valid or not. However, this function does not validate if email exist on this domain.

But I am trying to remove errors as much as possible so that our data is in proper format.

Actually, checking the existence of domain name simply ensures that there is no typo error from user input in email address domain name.


I hope by now you know how to validate email address using PHP. As you know you that it is very easy to validate email address format in PHP.

If you found this tutorial helpful, share it with your friends and developers group.

I spent several hours to create this tutorial, if you want to say thanks so like my page on Facebook, Twitter and share it.

Demo Download

Facebook Official Page: All PHP Tricks

Twitter Official Page: All PHP Tricks

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.

Leave a Reply

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