CrankBerry Blog Title
2009


No Comments

PHP Simple Email Validation

When having having forms on your website one of the most common form fields is usually the user's email. To ensure you're receiving quality information and not receiving bogus spams you should validate the email to be of a real one or close enough to. Here's a quick free email validation script for those PHP lovers.

Now this script does not verify domains, as there's no way to verify all domains in this world for a valid email address. What this function does is verify a few things:

  • No spacing in the entry
  • There are characters before the @ sign
  • There are characters after the @ sign
  • There is a period after the @ sign
  • After the last period there is some characters

And that is really how emails are composed of. For example:

  • some_characters@character.com
  • some_characters@character.co.jp

Well I won't bore you with the composition and dynamics of emails, here's the function you can add into your form processing script to verify email entries. Now, I mentioned this is a basic script because it does not validate special characters that aren't allowed in email names, maybe next time.

The Script Already

function verify_email($email) {
        //Trim
        $email = trim($email);
       
        //Var
        $email_error = TRUE;
       
        //Check Spacing
        if (@strpos($email, ' ')) {
                $email_error = FALSE;
        }
       
        //Validate Email
        $first_at = strpos($email, '@');
        if ($first_at<1) {
                $email_error = FALSE;
        } else {
                //Make the Last Part of the Email
                $email_end = substr($email, $first_at+1);
               
                //No More @ Symbol
                $more_at = strpos($email_end, '@');
                if ($more_at>=1) {
                        $email_error = FALSE;
                } else {
                        //Check Next Char isn't a period
                        $first_period = strpos($email_end, '.');
                       
                        if ($first_period<1) {
                                $email_error = FALSE;
                        } else {
                                //Last Part of the Email
                                $email_last = substr($email_end, $first_period+1);
                               
                                //Another Character
                                $email_len = strlen($email_last);
                               
                                if ($email_len<1) $email_error = FALSE;
                               
                        }
                }
        }
       
        return $email_error;
}

If you aren't sure how to use this script. Create a test.php page and copy the following script into it and try it out.

<?php

//Calling The Function
if (isset($_POST['test'])) {
        $email = $_POST['email'];
       
        //Is It Valid?
        if (verify_email($email)) echo '<strong>Email is Valid</strong>
'
;
        else echo '<strong>INVALID EMAIL</strong>
'
;
       
        echo '
'
;
}

function verify_email($email) {
        //Trim
        $email = trim($email);
       
        //Var
        $email_error = TRUE;
       
        //Check Spacing
        if (@strpos($email, ' ')) {
                $email_error = FALSE;
        }
       
        //Validate Email
        $first_at = strpos($email, '@');
        if ($first_at<1) {
                $email_error = FALSE;
        } else {
                //Make the Last Part of the Email
                $email_end = substr($email, $first_at+1);
               
                //No More @ Symbol
                $more_at = strpos($email_end, '@');
                if ($more_at>=1) {
                        $email_error = FALSE;
                } else {
                        //Check Next Char isn't a period
                        $first_period = strpos($email_end, '.');
                       
                        if ($first_period<1) {
                                $email_error = FALSE;
                        } else {
                                //Last Part of the Email
                                $email_last = substr($email_end, $first_period+1);
                               
                                //Another Character
                                $email_len = strlen($email_last);
                               
                                if ($email_len<1) $email_error = FALSE;
                               
                        }
                }
        }
       
        return $email_error;
}

?>

There you have, a simple email validation script. Leave some comments and feedback if you think there are ways to enhance this script.

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    Enter Email

    <input name="email" type="text" /> <input name="test" type="submit" value="Test" />
</form>

TL
This entry was posted on Wednesday, October 14th, 2009 at 12:47 am and is filed under PHP, Scripts and Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
BL

Leave a Reply

Spam protection by WP Captcha-Free