<

Validation of Email Addresses with Regular Expressions

Posted on
8,074 Points
1,274 Views
3 Endorsements
Last Modified:
Published
Experience Level: Beginner
4:56
Terry Woods
IT consultant and specialist in WordPress website management
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions.

Video Steps

1. Consider email address RFCs

2. Look at HTML5 form input element (with type=email) regex pattern

3. Try out the given pattern on some test data

4. Explain why we shouldn't be trying to match top level domains

5. Remind about server side validation

6. Remind about an expected level of knowledge

7. Mention EE Regular Expressions Topic Area

3
Author:Terry Woods
1 Comment
LVL 111

Comment

by:Ray Paseur
In addition to regular expressions, PHP has some built-in filters that can help.
http://php.net/manual/en/function.filter-var.php
http://php.net/manual/en/filter.filters.validate.php

Here is how I validate an email address.
<?php // demo/email_validation.php
/**
 * How to use a utility function to test for a valid email address
 * 'Valid' does not mean email will be received - parked domains are valid, but inactive
 *
 * http://php.net/manual/en/intro.filter.php
 * http://php.net/manual/en/function.checkdnsrr.php
 * https://tools.ietf.org/html/rfc5321
 *
 * See also:
 * https://www.experts-exchange.com/articles/3939/Registration-and-Email-Confirmation-in-PHP.html
 */
error_reporting(E_ALL);


function check_valid_email($email, $bogus=[], $route=TRUE)
{
    $valid = TRUE;

    // IF THE EMAIL STRING IS IMPROPERLY FORMED
    if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) $valid = FALSE;

    // IF THE DOMAIN IS IN OUR BLOCKED LIST
    foreach ($bogus as $badguy) {
        if (stripos($email, $badguy)) $valid = FALSE;
    }

    // IF THE DOMAIN HAS NO MAIL-EXCHANGE RECORD
    if ($route) {
        $domain = explode('@', $email);
        if ( !checkdnsrr($domain[1]) ) $valid = FALSE;
    }

    return $valid;
}


// INJECTED DEPENDENCY - LIST OF BAD DOMAINS
$bogus = [ '@unknown.com', '@example.com', '@gooseball.org' ];


// DEMONSTRATE THE FUNCTION IN ACTION
$e = !empty($_GET['e']) ? $_GET['e'] : NULL;
if ($e)
{
    if (check_valid_email($e, $bogus))
    {
        echo "<br/>VALID: $e" . PHP_EOL;
    }
    else
    {
        echo "<br/>BOGUS: $e" . PHP_EOL;
    }
}


// END OF PROCESSING - CREATE THE FORM USING HEREDOC NOTATION
$form = <<<ENDFORM
<form>
TEST A STRING FOR A VALID EMAIL ADDRESS:
<input name="e" value="$e" />
<input type="submit" />
</form>
ENDFORM;

echo $form;

Open in new window

1

Suggested Videos

Which language to choose for your next web project? Python, PHP, or maybe Node.js? Which framework to choose? Should you go with Symphony, Yii, or Laravel in PHP world? Django, Pyramid or Flask in Python world? Or maybe go for Feathers, Sails, Meteo…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month