Computing Magazine

PHP Email Validation

Posted on the 15 April 2012 by In7rud3r

 

PHP Email Validation

We have also use htmlspecialchars() to remove special/unwanted/malicious characters from the email.

eregi() is used to verify that the email is in proper format or not.

You can copy paste the code or write the following code in notepad on any other PHP editor.

 

function EmailValidation($email) {

$email = htmlspecialchars(stripslashes(strip_tags($email))); //parse unnecessary characters to prevent exploits
if ( eregi ( ‘[a-z||0-9]@[a-z||0-9].[a-z]‘, $email ) ){
//checks to make sure the email address is in a valid format
$domain = explode( “@”, $email ); //get the domain name
if ( @fsockopen ($domain[1],80,$errno,$errstr,3)) {
//if the connection can be established, the email address is probabley valid
return true;
}
else
{
return false; //if a connection cannot be established return false
}
}
else
{return false; //if email address is an invalid format return false
}
}
function EmailForm(){
if(empty($_POST['email'])){
echo “<form action=”.$_SERVER['PHP_SELF'].” method=’post’>
<table border=’0′>
<tr>
<td>Email</td>
<td><input name=’email’ type=’text’ id=’email’ /></td>
</tr> <tr>
<td> </td>
<td><input type=’submit’ name=’Submit’ value=’Validate’ /></td>
</tr>
</table>
</form>”;
}
elseif(isset($_POST['email'])) {
if(EmailValidation($_POST['email'])) {
echo “An email has been sent to you. Please follow the instructions to activate your account.”;
}
else
{
echo “Your email address appears to be invalid. Please try again.”;
}
}
else
{
echo “An error has occured, please contact the administrator.”;
}
}
EmailForm();
?>

<a href="http://www.yesadvertising.com">affiliate marketing</a>\


Back to Featured Articles on Logo Paperblog