Validate Email address in PHP with example

While taking E-mail address as user input, it's essential to validate it properly otherwise it can effect application functionality. In this article, we will validate Email address in PHP with regular expressions and also with FILTER_VALIDATE_EMAIL function. You can implement those methods in other PHP based frameworks like Laravel or CI.

Here, we will see two methods. Both checks URL is valid or not using PHP functionality.

  1. Validate Email Using Filter (FILTER_VALIDATE_EMAIL)
  2. Validate Email Using Regex

The Filter method is inbuilt in PHP. Which provides various filter validation like email, URLand more. While on other hand the preg_match() or regular expression is aimed to search pattern into string and return flag based on result. It will return True if match found otherwise, false.

To be clear, those methods will validate format of email address rather then valid email.

Validate Email Using Filter (FILTER_VALIDATE_EMAIL)

We can validate email address using filter_var() method and FILTER_VALIDATE_EMAIL filter. Here, function will take email address as argument and validate the email against the syntax in RFC 822. We can test the validation of email using a valid and an invalid email.

In this tutorial, we will define email address statically rather than form input. You can use it as per your requirement. Let's take an example to validate email address using this method:

In above example, we have created function that implements our logic to validate email. Here, we just print message based on validation. As you can see while calling this function first time it will show valid address. But when we call function using second email then it will show email address as invalid because it doesn't match RFC 822 syntax.

Validate Email Using Regex

Same as java-script or any other programming language, we can also use preg_match() function to validate email address. It will use the regular expression for the validation rule of the email. We can use this method and define custom rules for validation.

It will take two parameters, First one is regular expression and second one is our email address which we want to validate. Let's create same function as above example and validate email addresses.

It will print same output as above but here it will validate using regular expression. It will return invalid for second address because we added rule that domain name can not be numeric.


In this article, we have validated Email address using filter function and regular expression. The filter method is idle for this purpose because of flag and it's easy to implement.

Share your thoughts

Ask anything about this examples