Quantcast
Channel: Martech Zone
Viewing all articles
Browse latest Browse all 1361

How To Validate An Email Address With Regular Expressions (Regex)

$
0
0

Validate Email Address With Regular Expression Regex

Virtually every programming language supports regular expressions nowadays. While some developers don’t like them, they truly are a best practice as they typically perform functions like validation extremely fast with fewer server resources. Email addresses are a perfect example… where they can be easily checked to ensure they’re properly formatted.

Keep in mind that validation is not verification. Validation simply means that the data passed follows a standard format that is properly constructed. Some interesting things about email addresses that could be missed upon validation.

How Long Can An Email Address Be?

I had to do some digging today to find it, but did you know what the valid length of an email address is? It’s actually broken into parts… Name@Domain.com. This is according to RFC2822.

  1. Name can be 1 to 64 characters.
  2. Domain can be 1 to 255 characters.

That means that this could be a valid email address:

loremaipsumadolorasitaametbaconsectetueraadipiscin
gaelitanullamc@loremaipsumadolorasitaametbaconsect
etueraadipiscingaelitcaSedaidametusautanisiavehicu
laaluctuscaPellentesqueatinciduntbadiamaidacondimn
tumarutrumbaturpisamassaaconsectetueraarcubaeuatin
ciduntaliberoaaugueavestibulumaeratcaPhasellusatin
ciduntaturpisaduis.com

Try fitting that on a business card! Ironically, most email address fields are limited to 100 characters on the web… which is technically incorrect. Some of the other regular expressions used to validate email addresses also look for a 3-digit top-level domain, like .com; however, there’s no limitation to the length of top-level domains (eg. Martech Zone has 4 digits – .zone).

Email address standardization is far more complex than you realize. When written to the standard, here’s the true regular expression for an email address, credit to Regexr:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

HTML5 Doesn’t Even Need Validation

The easiest means to ensure an email is valid according to the standard is by using an HTML5 email input field:

<input type='email' name='email' placeholder='name@domain.com' />

There are times, though, that your web application will still want to validate the email address both in the browser when entered and when submitted to your server.

Regex For A Proper Email Address in PHP

Few people realize it, but PHP now has the RFC standard built into its filter validation function.

if(filter_var("name@domain.com", FILTER_VALIDATE_EMAIL)) {
    // Valid
}
else {
    // Not Valid
}

Regex For A Proper Email Address in Javascript

You don’t have to have an overly complex standard for checking an email address structure. Here’s a simple means using JavaScript.

function validateEmail(email) 
{
    var re = /\\S+@\\S+/;
    return re.test(email);
}

Of course, that’s not to the RFC standard, so you may wish to validate each section of the data to ensure it’s valid. This regular expression will comply with about 99.9% of email addresses out there. It’s not fully to standard, but it’s useful for virtually any project.

function validateEmail(email) 
{
  var re = /^(?:[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/;

  return re.test(email);
}

Credit for these examples goes to HTML.form.guide.

© 2021 DK New Media, LLC, All Rights Reserved


Viewing all articles
Browse latest Browse all 1361

Trending Articles