E-Mail Validation in PHP
In this tutorial you will learn how to validate E-mail addresses using PHP. When using E-mail ID fields in forms it is a good practice to validate the E-mail addresses using PHP code. The following example shows how to validate an E-mail address using PHP for Linux, Unix, Windows platforms.
Example:Sample PHP Code
<?php
//Check whether the submission is made
if(!isset($_POST["hidSubmit"])){ $stremail="";
DisplayForm();
}
else{
//Validate the Email Address
$stremail=$_POST["txtemail"];
$result=ereg("^[^@ ]+@[^@ ]+\.[^@ ]+$",$stremail,$trashed);
if(!$result){
echo "Enter a valid E-mail Address";
DisplayForm();
}
else{
echo "Invalid E-mail Address";
$stremail="";
DisplayForm();
}
}
//User-defined Function to display the form in case of Error
function DisplayForm(){
global $stremail;
?>
HTML code for the form follows..
Cut 'n' Paste HTML Code
HTML CODE
That's it you've learnt how to validate E-mail addresses using PHP code.