+ -
当前位置:首页 → 问答吧 → 一个ajax的验证例子

一个ajax的验证例子

时间:2007-10-15

来源:互联网

客户端页面check.html
复制PHP内容到剪贴板
PHP代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
    <head>
        <title>Ajax</title>
        <script type="text/javascript">
            var xmlHttp;
            
            function createXMLHttpRequest(){
                if(window.ActiveXObject) {
                    xmlHttp = new window.ActiveXObject("Microsoft.XMLHTTP");
                }else if(window.XMLHttpRequest) {
                    xmlHttp = new window.XMLHttpRequest();
                }
            }
            
            function sendData(what){
                createXMLHttpRequest();
                if(what == 'username'){
                    var datas = document.getElementById("username");
                    var URI = "check.php?username="+escape(datas.value);
                }else if(what == 'email'){
                    var datas = document.getElementById("email");
                    var URI = "check.php?email="+escape(datas.value);
                }
                xmlHttp.open("GET",URI,true);
                if(what == 'username')
                    xmlHttp.onreadystatechange = callback1;
                else if(what == 'email')
                    xmlHttp.onreadystatechange = callback2;
                xmlHttp.send(null);
            }
            
            function callback1(){
                if(xmlHttp.readyState == 4){
                    if(xmlHttp.status == 200){
                        var reback = xmlHttp.responseText;
                            document.getElementById("resultusername").innerHTML = reback;
                    }
                }
            }
            
            function callback2(){
                if(xmlHttp.readyState == 4){
                    if(xmlHttp.status == 200){
                        var reback = xmlHttp.responseText;
                            document.getElementById("resultemail").innerHTML = reback;
                    }
                }
            }
        </script>
    </head>
    
    <body style="text-align:center;font-family:arial;">
        <table style="margin:0 auto; border:1px black solid;width:400px;font-size:12px;">
            <tr>
                <td>Name:</td>
                <td>
                    <input type="text" name="username" id="username"  />
                </td>
                <td><span id="resultusername" style="font-family:arial;color:green;margin-left:10px;font-size:12px;"></span></td>
            </tr>
            <tr>
                <td>E-mail:</td>
                <td>
                    <input type="text" name="email" id="email"  />
                </td>
                <td><span id="resultemail" style="font-family:arial;color:green;margin-left:10px;font-size:12px;"></span></td>
            </tr>
        </table>
    </body>
</html>
服务端页面check.php
复制PHP内容到剪贴板
PHP代码:

<?php
    $readyUserName = array(
                                        "Tony",
                                        "Jay",
                                        "Jacket",
                                        "songsong",
                                        "Rose",
                                        "Tom"
                                        );
    $userName = $_GET['username'];
    $email = $_GET['email'];
    
    if($userName != ""){
        if(in_array($userName,$readyUserName)){
            echo 'This name is exist...';
        }else{
            echo 'You can use this name...';
        }
    }
    
    if($email != ""){
        $model = "/[a-zA-Z0-9_]{1,}@[a-zA-Z0-9_]{1,}\.[a-zA-Z0-9_]{1,}[a-zA-Z0-9_.]{0,}/";
        if(preg_match($model,$email)){
            echo 'This email is right...';
        }else{
            echo 'This email is wrong...';
        }
    }
?>

作者: liexusong   发布时间: 2007-10-15

简单,实用.

作者: samsung   发布时间: 2007-10-15

同楼上,简单,实用!

作者: hacksec   发布时间: 2007-10-15

很好的东西!谢谢。

作者: likai321   发布时间: 2007-10-15

在我本机上测试,不能用

作者: pets511   发布时间: 2007-12-06