How To Validate An Email Address With Regular Expressions (Regex). Sample HTML5, PHP, C#, Python, and Java Code.
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.
What Is An Email Address?
An email address, as defined by the Internet Message Format (RFC 5322), is made up of two main parts: a local part and a domain part. The local part comes before the @
symbol and the domain part comes after. Here’s an example of an email address: example@example.com
, where example
is the local part and example.com
is the domain part.
- Local – The local part of an email address may contain a combination of alphanumeric characters, periods, hyphens, plus signs, and underscores. It is typically used to identify a specific mailbox or account on a server.
- Domain – The domain part of an email address consists of the domain name and its top-level domain (TLD). The domain name is a string of characters that identifies the server that hosts the email account. The TLD specifies the type of entity responsible for the domain name, such as a country code (e.g.
.uk
) or a generic top-level domain (e.g..com
,.org
).
While this is the basic structure of an email address, the rules for what constitutes a valid email address are complex.
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… Local@Domain.com.
- Local can be 1 to 64 characters.
- Domain can be 1 to 255 characters.
That means that – technically – 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).
Regular Expressions
RegEx is a perfect method for testing an email address because of its programmatic structure. Regular expressions are widely used in programming languages and text editors and are often integrated into text-processing libraries or frameworks. They are supported by many programming languages, including Python, Java, C#, and JavaScript, among others.
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])?
This regular expression pattern matches the basic format of an email address, including alphanumeric characters, periods, hyphens, plus signs, and underscores in the username, followed by an @
symbol, followed by a domain name. It’s important to note that this pattern will only check the format of the email address and not the actual existence of the email address.
HTML5 Includes Email Structure 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 C#
Here’s basic validation of an email address in C#
using System;
using System.Text.RegularExpressions;
public class EmailValidator
{
public static bool IsValidEmail(string email)
{
string pattern = @"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$";
return Regex.IsMatch(email, pattern);
}
}
Practical use of this method:
string email = "example@example.com";
if (EmailValidator.IsValidEmail(email))
{
Console.WriteLine(email + " is a valid email address.");
}
else
{
Console.WriteLine(email + " is not a valid email address.");
}
Regex For A Proper Email Address in Java
Here’s basic validation of an email address in Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailValidator {
private static final Pattern VALID_EMAIL_ADDRESS_REGEX =
Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
public static boolean isValidEmail(String email) {
Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(email);
return matcher.find();
}
}
Practical use of this method:
String email = "example@example.com";
if (EmailValidator.isValidEmail(email)) {
System.out.println(email + " is a valid email address.");
} else {
System.out.println(email + " is not a valid email address.");
}
Regex For A Proper Email Address in Python
Here’s a basic validation of an email address in Python:
import re
def is_valid_email(email):
pattern = re.compile(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$')
return True if pattern.match(email) else False
Practical use of this method:
email = "example@example.com"
if is_valid_email(email):
print(f"{email} is a valid email address.")
else:
print(f"{email} is not a valid email address.")
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!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\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 some of these examples goes to HTML.form.guide.