用戶注冊演示程序
<! doctype html public "-//w3c//dtd html 4.0//en" "http://www.w3.org/tr/rec-html140/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312"> <title>ajax用戶注冊演示程序</title> <script language="網頁特效" type="text/網頁特效"> <!-- //創建函數 function createxmlhttp() { var request; var browser = navigator.appname; //使用ie,則使用xmlhttp對象 if(browser == "microsoft internet explorer") { var arrversions = ["microsoft.xmlhttp", "msxml2.xmlhttp.4.0", "msxml2.xmlhttp.3.0", "msxml2.xmlhttp","msxml2.xmlhttp.5.0"]; for (var i=0; i < arrversions.length; i++) { try { //從中找到一個支持的版本並建立xmlhttp對象 request = new activexobject(arrversions[i]); return request; } catch (exception) { //忽略,繼續 } } } else { //否則返回一個xmlhttprequest對象 request = new xmlhttprequest(); if(request.overridemimetype) { request.overridemimetype('text/xml'); } return request; } } //全局xmlhttp對象實例變量 var http = createxmlhttp(); //發送請求 function chkuser() { var url = "check.php教程"; //請求"checkusername" servlet var name = document.getelementbyid("username").value; url += ("?username="+escape(name)+"&oprate=chkuser"); http.open("get",url,true); http.onreadystatechange = processhttpresponse; http.send(null); return ; } //處理響應 function processhttpresponse() { if(http.readystate == 4) { if(http.status == 200) { var xmldocument = http.responsexml; if(http.responsetext!="該用戶名有效,可以使用!") { //返回的信息動態顯示 document.getelementbyid("showstr").style.display = ""; document.getelementbyid("username").style.background= "#ff0000"; document.getelementbyid("showstr").innertext = http.responsetext; } else { document.getelementbyid("username").style.background= "#ffffff"; document.getelementbyid("showstr").style.display = ""; document.getelementbyid("showstr").innertext = http.responsetext; } } else { alert("你所請求的頁面發生異常,可能會影響你浏覽該頁的信息!"); alert(http.status); } } } //檢驗輸入密碼 function chkpassword() { var m=document.form1; if(m.password.value.length>20 || m.password.value.length<6 ) { document.getelementbyid("passwordstr").style.display = ""; document.getelementbyid("password").style.background= "#ff0000"; document.getelementbyid("passwordstr").innertext = "對不起,密碼必須為英文字母、數字或下劃線,長度為6~20!"; } else { document.getelementbyid("password").style.background= "#ffffff"; document.getelementbyid("passwordstr").style.display = "none"; } } //驗證兩次密碼是否一致 function chkconfirmpassword() { var m=document.form1; if (m.password.value != m.confirmpassword.value) { document.getelementbyid("confirmpasswordstr").style.display = ""; document.getelementbyid("confirmpassword").style.background= "#ff0000"; document.getelementbyid("confirmpasswordstr").innertext = "對不起,密碼與重復密碼不一致!"; } else { document.getelementbyid("confirmpassword").style.background= "#ffffff"; document.getelementbyid("confirmpasswordstr").style.display = "none"; } } //驗證email是否有效 function chkemail() { var m=document.form1; var email = m.email.value; //正則表達式 var regex = /^([a-za-z0-9_-])+@([a-za-z0-9_-])+(.[a-za-z0-9_-])+/; var flag = regex.test(email); if(!flag) { document.getelementbyid("emailstr").style.display = ""; document.getelementbyid("email").style.background= "#ff0000"; document.getelementbyid("emailstr").innertext = "對不起,郵箱地址無效!"; } else { document.getelementbyid("email").style.background= "#ffffff"; document.getelementbyid("emailstr").style.display = "none"; } } //提交檢查函數 function submitcheck() { var m=document.form1; if(m.username.value.length==0) { alert("對不起,用戶名必須為英文字母、數字或下劃線,長度為5~20。"); m.username.focus(); return false; } if(m.password.value.length==0) { alert("對不起,密碼必須為英文字母、數字或下劃線,長度為5~20。"); m.password.focus(); return false; } if (m.password.value != m.confirmpassword.value) { alert("對不起,密碼與重復密碼不一致!"); m.confirmpassword.focus(); return false; } if(m.email.value.length==0) { alert("對不起,郵箱地址不能為空!!"); m.email.focus(); return false; } m.submit(); } //--> </script> <body > <form name="form1" method="post" action="register.php"> <h3 align="center">ajax用戶注冊程序</h3> <table align="center" width="500" border="1" > <tr> <td><font color="red">*</font></td> <td width="100">用戶帳號:</td> <td><input type="text" name="username" maxlength="20" style="background=#ffffff" onblur="chkuser()"></td> <td><div id="showstr" style="background-color:#ff9900;display:none"></div></td> </tr> <tr> <td><font color="red">*</font></td> <td>用戶密碼:</td> <td align="left"><input type="password" name="password" maxlength="22" style="background=#ffffff" onblur="chkpassword()"/> </td> <td><div id="passwordstr" style="background-color:#ff9900;display:none"></div></td> </tr> <tr> <td><font color="red">*</font></td> <td>確認密碼:</td> <td><input type="password" name="confirmpassword" maxlength="20" style="background=#ffffff" onblur="chkconfirmpassword()"/></td> <td><div id="confirmpasswordstr" style="background-color:#ff9900;display:none"></div></td> </tr> <tr> <td><font color="red">*</font></td> <td>email:</td> <td><input type="text" name="email" maxlength="100" style="background=#ffffff" onblur="chkemail()"></td> <td><div id="emailstr" style="background-color:#ff9900;display:none"></div></td> </tr> </table> <div align="center"> <input type="button" name="ok" value=" 確定 " onclick="submitcheck()"> <input type="reset" name="reset" value=" 取消 "> </form> </div> </body> </html>reg.php檢測程序 <?php header("content-type:text/html;charset=gb2312"); @mysql教程_connect('localhost','root','ebaeba') or die("數據庫教程服務器連接失敗"); @mysql_select_db("test") or die("數據庫不存在或不可用"); $uname = $_get['username']; //下面進行數據庫查詢 查找是不是有這一個用戶 //如果沒有查找到這個用戶名 $sql="select * from t1 where name='".$uname."'"; $query=mysql_query($sql); $row=mysql_fetch_object($query); if(strlen($uname)<6||strlen($uname)>20) { $msg="用戶名必須是6至20個字符."; } else { if($row==false) { $msg="該用戶名有效,可以使用!"; } else { $msg="對不起,此用戶名已經存在,請更換用戶名注冊!"; } } echo $msg ; ?>