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.
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:
/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
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.
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 protected]";
console.log(validateEmail(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 = "[email protected]"
if validate_email(email):
print("Valid email")
else:
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}$/
!!email.match(regex)
end
# Example usage:
email = "[email protected]"
if validate_email(email)
puts "Valid email"
else
puts "Invalid email"
end
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 = "[email protected]";
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 (
"fmt"
"regexp"
)
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 := "[email protected]"
if validateEmail(email) {
fmt.Println("Valid email")
} else {
fmt.Println("Invalid email")
}
}