00 Votes

PHP: Validate E-Mail Address

Question by Compi | 2015-05-03 at 22:07

I have a form on my website containing a field in which a user can enter his email address. Now, I would like to verify in my PHP script whether the user really has entered a valid and correct e-mail address or something else.

Is there any function in PHP with which you can check and validate e-mail addresses?

ReplyPositiveNegativeDateVotes
2Best Answer2 Votes

Since version 5.2.0 there is the handy function filter_var() available in PHP, which can inter alia be used to validate e-mail addresses.

I will give you an example of how to use the function:

$s = 'test@example.com';

if (filter_var($s, FILTER_VALIDATE_EMAIL)) {
   echo "$s is a valid email address!";
} else {
   echo "$s is not a valid email address!";
}

As a first parameter, you are passing of the e-mail address that should be checked, as second parameter, you have to specify the constant FILTER_VALIDATE_EMAIL. The result of filter_var() will be true if the passed string is a valid e-mail address, false otherwise.
2015-05-04 at 19:53

ReplyPositive Negative
00 Votes

I also recommend using filter_var() for validating e-mail addresses. If you should use an older version of PHP, of course, you are nevertheless forced to use a regular expression for the check.

Of course, you can quickly construct a regular expression like "#[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+#", which is often used but nonetheless produces errors in many cases.

PHP is internally using a quiet complex regular expression (see here), which I will demonstrate the following example code.

$reg = "/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD";

$email = 'test@example.com';

if (preg_match($reg, $email)) {
    // valid
}

Please note the following copyright notice for this regular expression that can also be found when following the link above:

The regex below is based on a regex by Michael Rushton.

However, it is not identical. I changed it to only consider routeable addresses as valid. Michael's regex considers a@b a valid address which conflicts with section 2.3.5 of RFC 5321 which states that:

Only resolvable, fully-qualified domain names (FQDNs) are permitted when domain names are used in SMTP. In other words, names that can be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed in Section 5) are permitted, as are CNAME RRs whose targets can be resolved, in turn, to MX or address RRs.  Local nicknames or unqualified names MUST NOT be used.

This regex does not handle comments and folding whitespace. While this is technically valid in an email address, these parts aren't actually part of the address itself.

Michael's regex carries this copyright:

Copyright © Michael Rushton 2009-10

http://squiloople.com/

Feel free to use and redistribute this code. But please keep this copyright notice.
2015-05-05 at 15:09

ReplyPositive Negative
Reply

Related Topics

PHP: Sending an E-Mail

Tutorial | 0 Comments

How to avoid Spam Mails

Tip | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.