FILTER_VALIDATE_EMAIL checks whether a value has a valid email-address format. A format check does not prove that the mailbox exists or can receive mail.
$email = 'user@example.com';
if (filter_var($email, FILTER_VALIDATE_EMAIL) !== false) {
echo 'Valid email format';
}
$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. 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
$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
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.