How to validate Email Id in PHP
In this tutorial, we will learn how to validate an Email id in PHP using the simplest way.
We can validate the Email Id using Two methods:
1. Using the filter_var() Method
2. Using the preg_match() Method
1. Using the filter_var() Method
The filter_var()
function in PHP is a versatile function used for validating and sanitizing data. It is commonly used for validating email addresses, URLs, and other input data.
The basic syntax of filter_var()
is:
1 2 |
filter_var($variable, $filter, $options); |
$variable
: The variable or value you want to filter/validate.$filter
: The filter to apply to the variable.$options
(optional): Additional options for the filter.
Here are a few commonly used filters with filter_var()
:
FILTER_VALIDATE_EMAIL
: Validates if a value is a properly formatted email address.FILTER_VALIDATE_URL
: Validates if a value is a properly formatted URL.FILTER_VALIDATE_IP
: Validates if a value is a valid IP address.FILTER_VALIDATE_INT
: Validates if a value is an integer.FILTER_VALIDATE_BOOLEAN
: Validates if a value is a boolean (true or false).
The filter_var()
function returns the filtered value if the validation is successful, or false
if the validation fails. If the $options
parameter is specified, it can modify the behavior of the filter.
Here’s an example of using filter_var()
to validate an email address:
1 2 3 4 5 6 7 8 |
$email = "example@example.com"; if (filter_var($email, FILTER_VALIDATE_EMAIL)) { echo "Email address is valid."; } else { echo "Invalid email address format."; } |
In this example, filter_var($email, FILTER_VALIDATE_EMAIL)
validates if the $email
variable contains a valid email address. If it returns true
, it means the email address is valid. Otherwise, it returns false
, indicating an invalid email address.
2. Using the preg_match() Method
You can also validate an email address using the preg_match()
function in PHP, which allows you to perform regular expression pattern matching. Here’s an example of how you can use preg_match()
to validate an email address:
1 2 3 4 5 6 7 8 9 10 11 |
$email = "example@example.com"; // Regular expression pattern for email validation $pattern = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/"; if (preg_match($pattern, $email)) { echo "Email address is valid."; } else { echo "Invalid email address format."; } |
In this example, the regular expression pattern /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
is used to validate the email address format. This pattern checks for the following conditions:
- The email address should start with one or more alphanumeric characters, dots, underscores, percent signs, plus signs, or hyphens.
- It should be followed by the
@
symbol. - The domain name should contain one or more alphanumeric characters, dots, or hyphens.
- The domain should end with a dot and at least two alphabetical characters (e.g.,
.com
,.net
,.org
, etc.).
If the preg_match()
function returns a match, it means the email address is valid. Otherwise, it returns false
, indicating an invalid email address format.
Keep in mind that this regular expression pattern provides a basic level of email validation, but it does not guarantee that the email address exists or is deliverable. For a more comprehensive email validation, you may consider using a dedicated email validation library or service.