Validate emails the PHP 5 way

Forget the numerous regex’s, if you’re using PHP5 (i hope 99% of you do…), just use PHP’s native filter_var function:

1
2
3
4
5
6
7
8
9
function is_email_valid($email) {
  return filter_var(filter_var($email, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
}

if (is_email_valid('email@example.com')) {
  /* yeah! we've got a valid email address */
} else {
  /* oh noes! invalid email */
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>