<

Validation of Email Addresses with Regular Expressions

Posted on
8,075 Points
1,275 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

Title Views Activity
Introduction to Accessibility (Part 1) 24
Introduction to Accessibility (Part 2) 25
Introduction to jQuery (Part 1) 853
Lists and Links 106
I always make backups of my website before upgrading WordPress, but this time, I decided not to bother and sure enough, Murphy's law struck - it broke my website! This article explains how I fixed it without a backup. The process wasn't easy to find…
In today's digital age, where online transactions and interactions have become the norm, encountering errors while browsing the internet is not uncommon. One such error is the "419 Page Expired" message, which can be frustrating and confusing for us…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month