By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Validate e-mails with regular expressions in PHP

Tutorial on how to validate e-mails using regex patterns in PHP.

Edited: 2012-10-25 15:02

First you need to determine with yourselves if validating emails is really necessary. There are many reasons not to validate emails, by far the best reason not to validate your users e-mail addresses, is that e-mails addresses are completely anonymous.

There is just no way of telling if the user behind the email, is who they claim to be. If you really need the users personal information for something, then its best to rely on other verification methods.

If you just need to check if a e-mail is valid, so that you don't send mails to non-existant e-mail addresses, etc. Then you will find that its almost entirely up to you, what characters you want to allow in e-mail addresses. Making a Regular Expression to accept all valid addresses, may end up slipping trough a lot of non-existent made up ones, simply because users generally don't use special characters in their e-mail.

E-mail regex patterns

There are a few patterns that we want to cover in this Tutorial, the first would accept any address containing letters and numbers, including underscores and hyphen characters. Whats important to keep in mind here, is that things change all the time, tomorrow we might have entirely different TLDs then we do today. But this is not reason enough to support them all, or even the valid ones that we do have today.

The below pattern will only accept a TLD in lowercase letters, between 2-3 characters in length. This would invalidate TLDs like info and museum, but then again, have you ever personally seen someone using emails from these domains?


To make the pattern match e-mails based on those domains, you could simply replace the {2,3} with a plus + sign, the plus sign can be translated to 1 or more.


You can use this pattern with preg_match to easily validate emails on the server-side, this is demonstrated below.

$email = "";
if (preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9-]+.[a-z]+$/", $email)) {
    echo "The e-mail address was considered valid.";
} else {
    echo "The e-mail address you entered is not considered valid on this website, this is done to limit fake e-mails. Etc.";