<

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
Adding a Business to Google Maps 144
Migrating the Website 127
Introduction to jQuery (Part 1) 853
Control Phase - 5.3 Six Sigma Control Plans 36
When employers are recruiting, they’ll be looking for applicants who are strong in two specific skill sets. While hard skills are highly desirable, you will also need to show you have the soft skills that make you a valuable team player.
Data can be kept in different media, Sometimes, the data need to be extracted, transformed or loaded in different ways. For this article, I'm going to demonstrate some of the popular ways of importing JSON data into MS SQL Server.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month