Validating an email address with PHP
By admin
Steps to validate an email address:
- Sanitizing the email address:
- strtolower($emailAddress);
- filter_var($emailAddress, FILTER_SANITIZE_EMAIL); // remove bad characters from the email.
- Validating the email adress:
- filter_var($emailAddress, FILTER_VALIDATE_EMAIL);
Note: For email addresses containing Internationalized Domain Names (IDN) you need to convert it to punycode before validating the email address.
<pre class="lang:php decode:true ">function validateEmail($emailAddress) {
$emailAddress = strtolower($emailAddress);
$sanitilzedEmail = filter_var($emailAddress, FILTER_SANITIZE_EMAIL);
if ($emailAddress == $sanitilzedEmail && filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
return $emailAddress;
} else {
return false;
}
}