Email validation within a form in PHP

Enter any email address

We are not storing your submitted email address, you can check by using correct formatted email only.
Example abcd@example.com OR asfasf


Email Validation 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.
This code can be used as a part of the other entered data validation used. You can read the php form validation here.

The HTML FORM for user to input email

<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>

How to receive email address

Visitors enter their email address in a textbox kept inside a web form.
From a web form we will get email address either by GET or POST method to the processing script. For ready reference here are two ways to get the email address from a web 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>";
}
eregi is DEPRECATED in PHP 5.3.0 and removed in PHP 7.0.0
Better use preg_match

Checking presence of @ and .

Minimum requirement of any email address is presence of @ and one dot ( . ) . Here we can check for the presence of these two conditions and clear or validated the variable in the presence of these two chars. Note that many complex conditions were this checking will fail but to maintain simplicity this can be used.

if (!stristr($em,"@") OR !stristr($em,".")) {
$msg="Your email address is not correct <BR>"; 
$status= "NOTOK";
} else {
echo " Your email address is OK ";
}

Using filter in PHP

PHP has built in filter and it is enabled for PHP version 5.2.0 onwards. By using filter we can check various other type of data including email address.
FILTER_VALIDATE_EMAIL Email validation using Ajax & PHP
PHP Form
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    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.

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer