<

Validation of Email Addresses with Regular Expressions

Posted on
7,531 Points
731 Views
3 Endorsements
Last Modified:
Experience Level: Beginner
4:57
Terry Woods
Bridging the gap, in a way you can understand, between what you want and tech that can achieve it. Websites, web based software, email...
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
Comment
Author:Terry Woods
1 Comment
 
LVL 111

Expert 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

0

Featured Post

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Join & Write a Comment

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month