FILTER_VALIDATE_EMAIL
PHP FILTER
$email='userid@domain.com';
if(filter_var($email,FILTER_VALIDATE_EMAIL)){
echo " Yes validation passed ";
}else{
echo " No validation failed ";
}
We can check email address for validation by using FILTER_VALIDATE_EMAIL filter in PHP. Here is a sample code.
In the above code in place of FILTER_VALIDATE_EMAIL we can use filter ID as 274
Validation of Email by GET method.
Whenever we receive any email address we can directly check like this ( without assigning any variable )
if(filter_var($_GET['email'],FILTER_VALIDATE_EMAIL)){
echo " Yes validation passed ";
}else{
echo " No validation failed ";
}
Inside our script before using email address we can check and assign a variable only if email validation is passed.
if(filter_var($_GET['email'],FILTER_VALIDATE_EMAIL)){
$email = $_GET['email'];
}else{
echo " Wrong email address ";
// stop execution or ask to re-submit
}
You can read how regular expressions is used to validate email address
FILTER_SANITIZE_EMAIL
The id of this filter is 517. We can sanitize any variable with email address to remove unwanted chars including blank space. This function FILTER_SANITIZE_EMAIL removes all except letters, digits and {|}!#/=$%*[]+-?^_`~&'@.
Here is an example
$str='use= rid@example.com41!5~6+7'; // Change this value to get different result
$str = filter_var($str,FILTER_SANITIZE_EMAIL);
echo $str;
Output is here
use=rid@example.com41!5~6+7
← Filter reference
Validating URL →
Validating Boolean data →
Ctype_alnum to check alphanumeric characters →
Validating Email address →
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com