≡ Menu

How to validate email addresses using PHP?

The question – I need a fast way to check if the entered email address is a valid email address using PHP. Is there a PHP code syntax that can be used to achieve this?

PHP has a command called filter_var. This command can be used to validate email addresses, urls and several other useful fields.

filter_var

To validate email addresses, use the following PHP script:

<?php
$email = "email@example.com";
$valid = filter_var($email, FILTER_VALIDATE_EMAIL);
if($valid == TRUE)
{
	echo "Entered Email Address was valid!";
}
else
{
	echo "Entered Email Address was invalid!";
}
?>

The FILTER_VALIDATE_EMAIL flag instructs filter_var to check if the passed in string is a valid email address.

There are several other flags that you can pass to filter_var:

FILTER_VALIDATE_URL - to validate URLs
FILTER_VALIDATE_IP - to validate IP addresses

Here is a full list of them!

Comments on this entry are closed.