Method 1: Email validation using regular expression.
<?php
// PHP program to validate email
// Function to validate email using regular expression
function email_validation($str) {
return (!preg_match(
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $str))
? FALSE : TRUE;
}
// Function call
if(!email_validation("author@geeksforgeeks.com")) {
echo "Invalid email address.";
}
else {
echo "Valid email address.";
}
?>
<?php
// Declare variable and initialize
// it to email
$email = "author@company.com";
// Validate email
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo("$email is a valid email address");
}
else {
echo("$email is not a valid email address");
}
?>
<?php
// Declare variable and store it to email
$email = "author.gfg@GeeksforGeeks.com";
// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate Email
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo("$email is a valid email address");
}
else {
echo("$email is not a valid email address");
}
?>