PHP 5.2 Validate an Email Address with the filter_var() Function

October 21st, 2011 by Regaz

From Version 5.2 PHP has Introduced a new Validating Tool: the filter_var() function.
Here is shown How to easily Validate an Email Address in a PHP Form with this function.
The filter_var() function Serve to Filter Variables Content using a given form of Filter. The Second Argument taken by the function is Right a Constant indicating which kind of Filtering the function need to Execute. For a List of Filters look Here.
In our case the Filter Constant for an Email validation is: FILTER_VALIDATE_EMAIL.
Here is an example of Email Validation:

filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)

This will Return True if the Submitted Email is well Shaped otherwise it will Return False.

Tags: