Validate Email Address With Regular Expression in PHP

In creating an registration form or feedback form, user has to enter email address on the form which needs to validate manually. If we will not validate for a proper email address, your web form will be the victim of spam registrations. 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 it with the help of a regular expression. It checks whether the email address follows a valid rule or not.

An email id of the form [email protected] is a valid email address.

See the code below.

<?php
if (isset($_POST['posted'])) {
$email = $_POST['email'];
$results = ereg("^[^@ ][email protected][^@ ]+.[^@ .]+$", $email, $trashed);
if ($results) {
$isamatch = "Valid";
}
else {
$isamatch = "Invalid";
}
echo "Email address validation says $email is " . $isamatch;
}
?>

This was the sample code that will validate the email address and then print whether the email address is valid or not. You can use it a function to return true or false after checking for a valid email address.

PHP also has a built in email validation function. You can also use that function. See the code below:

<?php
$email='[email protected]'
if(filter_var($email, FILTER_VALIDATE_EMAIL))
 {
 echo("E-mail is valid");
 }
else
 {
 echo("E-mail is not valid");
 }
?>

 


Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Part of Techlomedia Internet Pvt Ltd Developed By Deepanker