php+js+mysql设计的仿webQQ-邮箱验证

最近用php+js+mysql做了一个仿webQQ的课程设计,收获很多,现在将关键的技术总结一下,供大家学习交流。

<1>邮箱验证

用户在注册的时候,会在文本框里输入邮箱,这个时候通过文本框的onblur和onchange事件用Ajax无刷新技术来判断用户输入的邮箱是否合法以及是否与已注册的邮箱冲突。

Js代码

function checkEmail(Email)
{       
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    if(xmlhttp.overrideMimeType)
    {//设置MIME类别
       xmlhttp.overrideMimeType("text/xml");
    }
  }
else
  {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  var url="checkEmail.php?email="+document.getElementById("email").value;    //转到checkEmail.php进行验证
  xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {  
           document.getElementById("error1").innerHTML=xmlhttp.responseText;
    }
  } 
xmlhttp.send(null);
}

PHP代码

<?php
header('Content-Type:text/html;charset=GB2312');    //编码方式设置
include("conn.php");
$email=$_GET["email"];
$len=strlen($email);
if($email==null)
  {
         echo "<font color=red size=2px>*邮箱不能为空!</font>";
  }
else
  {
     if($len>50)
           {
                   echo "<font color=red size=2px>*邮箱不要超过50个字符!</font>";
           }
         else
           {
                   if(eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$",$email))    //在php中用正则表达式验证邮箱
                     {
                            $sql="select * from user where email='$email'";     //连接数据库进行查询看邮箱是否被用
                $result = mysql_query($sql);
                $num=mysql_num_rows($result);
                    if($num>0)
                      {
                                         echo "<font color=red size=2px>*该邮箱已被用!</font>";
                                  }
                            else
                                  {
                                     echo "<font color=green size=2px>*邮箱可用!</font>";
                                  }
        
                         }
                   else
                     {
                                echo "<font color=red size=2px>*该邮箱不可用!</font>";
                         }       
           }  
  }
?>

通过对邮箱验证的学习,我想其他的验证应该很简单了吧!(未完待续)