Last Updated: February 25, 2016
·
788
· dayjo

PHP - Stop using RegEx for email validation

Save yourself a lot of time and stress fiddling with RegEx to work with edge cases.

Instead, use PHP's built in filter_var method like so;

<?php
$email = 'my.email+is-weird@domain-name.com';
if ( filter_var($email, FILTER_VALIDATE_EMAIL) ) { 
 echo "Valid";
} else {
 echo "Invalid";
}

References & Documentation:
http://www.php.net/manual/en/filter.examples.validation.php
http://www.php.net/manual/en/function.filter-var.php