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?
If you want to check if provided domain from email address is valid, use something like:
This is handy way to filter a lot of invalid email addresses, along with standart email validation, because valid email format does not mean valid email.
Note that
idn_to_ascii()
(or his sister functionidn_to_utf8()
) function may not be available in your PHP installation, it requires extensions PECL intl >= 1.0.2 and PECL idn >= 0.1.Also keep in mind that IPv4 or IPv6 as domain part in email (for example
user@[IPv6:2001:db8::1]
) cannot be validated, only named hosts can.See more here.
Nowadays, if you use a HTML5 form with
type=email
then you're already by 80% safe since browser engines have their own validator. To complement it, add this regex to yourpreg_match_all()
and negate it:Find the regex used by HTML5 forms for validation
https://regex101.com/r/mPEKmy/1
You can use filter_var for this.