Email validation with filter_var

by admin on juli 26, 2011

When having a website that allows user-registration it’s good to check that the user data inputed is valid. A lot of people use RegExp to check if an email is valid. This is generally not the best way to do it. I recently came across a good way to do e-mail verification by using the php filter_var function.

This is how it’s done

$email = "someone@exa mple.com";

if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
  echo "E-mail is not valid";
  // Rest of PHP-code
  }
else
  {
  echo "E-mail is valid";
  // Rest of PHP-code
  }

Leave your comment

You must be logged in to post a comment.