Sunday, 8 April 2012

validate email address with regular expression in php



creating an registration form or feedback form, user has to enter, email address on the form which needs to validate manually. This is a good practice for making the application secure and your data from spams. For checking an email address to be valid or not, you have to check is against a regular expression.
An email id of the form id@example.com is a valid email address.
See the code below.




?php


if (isset($_POST['posted'])) {


$email = $_POST['email'];
$results = ereg(“^[^@ ]+@[^@ ]+.[^@ .]+$”, $email, $trashed);
if ($results) {
$isamatch = “Valid”;
} else {
$isamatch = “Invalid”;
}


echo “Email address validation says $email is ” . $isamatch;
}


?>

1 comment: