How to Validate Email Addresses with Regex

3 min read | November 11, 2023

Regex (regular expressions) been the go-to tool for string pattern matching for many years. One of the most common use cases is the validation of email addresses as part of the form validation process. We will go through on how to do this and why you should not only rely on regex as a software developer.

Regex Email Validation

Limitations of email validation with regex

An email address always consists of the following format: {string}@{string}.{tld}. There is a standard that specifies the format of email addresses: RFC 5322.

Looking into the standard one quickly realizes that there is a a wide range of valid email formats that are far more complicated than the common patterns that we see 90% of the time. Implementing all edge cases into this tutorial would go beyond the scope of this article. Therefore, our approach provides a good balance between complexity and practicality for most web applications.

Forming the regular expression

Forming the pattern that we have described in the last paragraph into a regular expression, we end up with the following: The strings before and after the @ may consist of letters, numbers, dots, dashes and underscores.

  • ^: assumes the start of the line.
  • [a-zA-Z0-9._-]+: The two string parts before and after the @ may consist of letters, numbers, dashes and underscores.
  • @: looking for the @ inside the string.
  • [a-zA-Z0-9.-]+: Like the first part of the email address, the second part equally consists of the same elements.
  • \.: Requiring the dot.
  • [a-zA-Z]{2,6}: Validates the top-level domain by allowing it to be between 2 and 6 characters long and consisting of letters only.

Finally, after putting it all together, we end up with the following regex:


Why you should not only rely on regex

Regex is a great tool and should be used. However, it simply checks whether the string pattern matches the typical specifications of an email address and does not check whether the inbox behind an email address actually exists.

ℹ️ Tip: An email validation API is a great tool for checking whether an email address exists.

Email Regex in JavaScript

The following sample snippet provides a simple on how to check an email address in vanilla JavaScript:

function validateEmail(email) {
    var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
    return regex.test(email);

// Example usage:
var email = "";

Email Regex in Python

In order to check whether an email address is valid in python, we can use the following snippet. Note: the r before the regex denotes a raw string in Python, that tells Python to ignore escape sequences within the string.

import re

def validate_email(email):
    regex = r'^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$'
    return re.match(regex, email)

# Example usage:
email = ""
if validate_email(email):
    print("Valid email")
    print("Invalid email")

Email Regex in Ruby

And another example using our regex to validate emails in ruby:

def validate_email(email)
  regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/

# Example usage:
email = ""
if validate_email(email)
  puts "Valid email"
  puts "Invalid email"

Email Regex in PHP

PHP is still one of the most commonly used server-side programming languages today. Hence, it is used for email checking tasks very often.

function validateEmail($email) {
    $regex = '/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/';
    return preg_match($regex, $email);

// Example usage:
$email = "";
if (validateEmail($email)) {
    echo "Valid email";
} else {
    echo "Invalid email";

Email Regex in Go

And finally a golang snippet to verify email addresses:

package main

import (

func validateEmail(email string) bool {
	regex := `^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$`
	return regexp.MustCompile(regex).MatchString(email)

func main() {
	email := ""
	if validateEmail(email) {
		fmt.Println("Valid email")
	} else {
		fmt.Println("Invalid email")

Start using our email validation software today!

Get 100 validations per month for free