Thursday, 20 October 2011

Email Validataion With PHP AJAX


1) put this code in it one html or php file.

<script type="text/javascript">
function ValidateEmail(email)
{
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateck()
{
if(httpxml.readyState==4)
{
document.getElementById("msg").innerHTML=httpxml.responseText;
}
}
var url="ajax.php";
url=url+"?email="+email;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);

}
</script>
<form name="f1" action="" method="post">
 Email Address
  <input type="text" name="email" onBlur="ValidateEmail(this.value);" />
  <div id="msg"></div>

  <input type="button" value="Submit" />
</form>

2) put this code in ajax file

<?php
error_reporting(0);
$email=$_GET['email'];
if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email)){
echo "<font color=red> Invalid email</font>";
}else{
echo "<font color=green> Valid Email</font>";}
?>

1 comment: