PHP Tutorials

PHP E-Mail Validation

E-Mail Validation in PHP

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:

E-Mail Validation

Enter an E-mail Address


Output: Error Enter a valid E-mail Address

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.

Please like, +1, link to and share this SmartWebby resource if you found it helpful. Thanks!