I have this function to validate an email addresses:
function validateEMAIL($EMAIL) {
$v = "/[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/";
return (bool)preg_match($v, $EMAIL);
}
Is this okay for checking if the email address is valid or not?
In my experience,
regex
solutions have too many false positives andfilter_var()
solutions have false negatives (especially with all of the newer TLDs).Instead, it's better to make sure the address has all of the required parts of an email address (user, "@" symbol, and domain), then verify that the domain itself exists.
There is no way to determine (server side) if an email user exists for an external domain.
This is a method I created in a Utility class:
The easiest and safest way to check whether an email address is well-formed is to use the
filter_var()
function:Additionally you can check whether the domain defines an
MX
record:But this still doesn't guarantee that the mail exists. The only way to find that out is by sending a confirmation mail.
Now that you have your easy answer feel free to read on about email address validation if you care to learn or otherwise just use the fast answer and move on. No hard feelings.
Trying to validate an email address using a regex is an "impossible" task. I would go as far as to say that that regex you have made is useless. There are three rfc's regarding emailaddresses and writing a regex to catch wrong emailadresses and at the same time don't have false positives is something no mortal can do. Check out this list for tests (both failed and succeeded) of the regex used by PHP's
filter_var()
function.Even the built-in PHP functions, email clients or servers don't get it right. Still in most cases
filter_var
is the best option.If you want to know which regex pattern PHP (currently) uses to validate email addresses see the PHP source.
If you want to learn more about email addresses I suggest you to start reading the specs, but I have to warn you it is not an easy read by any stretch:
Note that
filter_var()
is as already stated only available as of PHP 5.2. In case you want it to work with earlier versions of PHP you could use the regex used in PHP:P.S. A note on the regex pattern used above (from the PHP source). It looks like there is some copyright on it of Michael Rushton. As stated: "Feel free to use and redistribute this code. But please keep this copyright notice."
If you're just looking for an actual regex that allows for various dots, underscores and dashes, it as follows:
[a-zA-z0-9.-]+\@[a-zA-z0-9.-]+.[a-zA-Z]+
. That will allow a fairly stupid looking email liketom_anderson.1-neo@my-mail_matrix.com
to be validated.I think you might be better off using PHP's inbuilt filters - in this particular case:
It can return a true or false when supplied with the
FILTER_VALIDATE_EMAIL
param.This will not only validate your email, but also sanitize it for unexpected characters:
Answered this in 'top question' about emails verification https://stackoverflow.com/a/41129750/1848217
So, just check @, hint user on frontend and send verification emails on given address.