Tutorial: Validate an email address with PHP.

[Posted 12/26/2008 at 12:00 PM | No Comments]

To validate an email using PHP is very simple and extremely easier to incorporate into your already made PHP form. There are a lot of reasons for having your form validate the email address. The most obvious one would be that you gather the proper information and the user does not make any simple mistakes. You might also have a user registration script, an online newsletter or a simple contact form. If the user forgets to put in a @ sign then the script will catch it and throw back an error message.

One php page named verify.php will contain all the code. Let’s create a simple contact form.

HTML CODE:

<form action="verify.php" enctype="multipart/form-data" method="post">Name:
<input name="name" type="text" />
Email:

<input name="email" type="text" />

Comments:

<textarea cols=”20″ rows=”2″ name=”comments”></textarea>

<input name=”process” type=”hidden” value=”1″ />

<input type=”submit” value=”Verify!” />

</form>

We have three fields that the user can fill out: name, email and comments.

We also have a hidden field called “process” with the value of 1. This field will allow our script to check if the form has been submitted as you will see later.

Our form is done and now we have to code our PHP script. Just so we stick to the topic, I am going to ignore the name and comments field and keep my attention to the  email field. With a success, it will display “Yay! You entered a valid email address”.

PHP CODE:

if($_POST['process']==1)
{
    // IF THE EMAIL FIELD WAS LEFT BLANK

    if(empty($_POST['email']))

    {

        echo ‘<p style=”color:#C00″>Please enter your email address.</p>’;

    }

    // IF USER ENTERED SOMETHING INTO THE EMAIL FIELD

    else

    {

        // IF USER ENTERED AN INVALID EMAIL ADDRESS

        if(preg_match(‘/.*@.*\..*/’, $_POST['email']) > 0)

        {

            echo ‘Yay! You entered a valid email address!’;

        }

        // IF USER ENTERED A VALID EMAIL ADDRESS

        else

        {

            echo ‘<p style=”color:#C00″>Please enter a valid email address.</p>’;

        }

    }

}

if($_POST[‘process’]==1) checks if the form has been submitted.

If the form has been submitted, then it makes sure the user did not leave the email field blank by using the empty function.

The preg_match function performs the email address validation, it searches what the user entered as an email address and checks if it matches the regular expression. If preg_match returns a 0 it means there was no match. If it returns a 1 it means there is at least one match.

Of course, the code does not let you know if the email address actually exists. It only makes sure you entered an email address in a valid format which would be the email, then the @ character, then the domain name, then a period and then something else (email@domain.extension).


HELP SPREAD THE WORD!

Random Posts

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Please leave your comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.