In an input field we can verify the email address entered is in correct format or not using PHP eregi function. This is a part of php form validation we do for other entries to check proper formatted data is entered by the user or not.
<form method=post action=''>Enter any email address
<input type=text name=email value=$email>
<input type=submit value=Check>
<input type=hidden value=test name=todo>
</form>
$email=$_GET['email']; // GET method
$email=$_POST['email']; // POST method
Here is the code of this validation in PHP, using preg_match()
if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^",$email))
{
echo "<center><font face='Verdana' size='2' color=red>Invalid email</font></center>";
}else{
echo "<center><font face='Verdana' size='2' color=green>Valid Email</font></center>";
}
Same validation is done by using eregi()
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
echo "<center>Invalid email</center>";
}else{
echo "<center>Valid Email</center>";
}
if (!stristr($em,"@") OR !stristr($em,".")) {
$msg="Your email address is not correct <BR>";
$status= "NOTOK";
} else {
echo " Your email address is OK ";
}
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.
| good | 22-05-2013 |
| its interestiing..!! | |
| albusaidy | 16-11-2014 |
| worked great for me.. I love the fact that the page I was working on stayed on screen, and script showed where change needed to be made. Awesome Is there a way to make sure people do not enter www. in front of their email, it happens often. | |
| John S | 17-04-2019 |
| Hello, eregi() is deprecated as of PHP 5.3 Thanks. | |