1. 程式人生 > >ASP+ajax實用使用者唯一性檢查和註冊

ASP+ajax實用使用者唯一性檢查和註冊

程式碼如下:

reg.asp:

<!--#includefile="../inc/web_conn.asp"-->
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
<title>Kenvi.cn</title>
<METAname="keywords"content="kenvi"/>
<METAname="description"content="kenvi.cn"/>
<linkhref="zp.css"rel="stylesheet"type="text/css"/>
<scriptlanguage="javascript"src="/inc/main.js"></script>
<styletype="text/css">
body{
margin-left:0px;
margin-top:0px;
margin-right:0px;
margin-bottom:0px;
}
.dd{width:200px;text-align:center;}
.STYLE2{
font-size:14px;
font-weight:bold;
color:#FFFFFF;
}
.z0056C2{color:#0056C2;}
</style>
<scriptlanguage="javascript">
<!--
varmsg;
varli_ok='images/li_ok.gif';
varli_err='images/li_err.gif'
varbname_m=false;
msg=newArray(
"推薦使用:·
網易
郵箱 ·QQ郵箱 ·SOHU郵箱",
"請輸入4-14位字元,英文、數字的組合。",
"請輸入6位以上字元,不允許空格。",
"請重複輸入上面的密碼。",
"請輸入郵箱地址"
)
//document.getElementById("d_email").innerHTML=msg[0];
//document.getElementById("d_pass").innerHTML=msg[2];
//document.getElementById("d_pass2").innerHTML=msg[3];
//document.getElementById("d_name").innerHTML=msg[1];
varxmlHttp=false;
try{
xmlHttp=newActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
}catch(e2){
xmlHttp=false;
}
}
if(!xmlHttp&&typeofXMLHttpRequest!='undefined'){
xmlHttp=newXMLHttpRequest();
}

functionon_input(objname){
varstrtxt;
varobj=document.getElementById(objname);
obj.className="d_on";
//alert(objname);
switch(objname){
case"d_email":
strtxt=msg[4];
break;
case"d_name":
strtxt=msg[1];
break;
case"d_pass":
strtxt=msg[2];
break;
case"d_pass2":
strtxt=msg[3];
break;
}
obj.innerHTML=strtxt;
}

functionout_email(){
varobj=document.getElementById("d_email");
varstr=document.getElementById("ys_email").value;
varchk=true;
if(str==''||!str.match(/^[\w\.\-]
[email protected]
([\w\-]+\.)+[a-z]{2,4}$/ig)){chk=false}
if(chk){
obj.className="d_ok";
obj.innerHTML='電子郵箱地址已經輸入。';
document.getElementById("d_email_img").src=li_ok;
}else{
obj.className="d_err";
obj.innerHTML=msg[4];
document.getElementById("d_email_img").src=li_err;
}
returnchk;
}

functionout_upwd1(){
varobj=document.getElementById("d_pass");
varstr=document.getElementById("ys_pass").value;
varchk=true;
if(str==''||str.length<6||str.length>14){chk=false;}
if(chk){
obj.className="d_ok";
obj.innerHTML='密碼已經輸入。';
document.getElementById("d_pass_img").src=li_ok;
}else{
obj.className="d_err";
obj.innerHTML=msg[2];
document.getElementById("d_pass_img").src=li_err;
}
returnchk;
}

functionout_upwd2(){
varobj=document.getElementById("d_pass2");
varstr=document.getElementById("ys_pass2").value;
varchk=true;
if(str!=document.getElementById("ys_pass").value||str==''){chk=false;}
if(chk){
obj.className="d_ok";
obj.innerHTML='重複密碼輸入正確。';
document.getElementById("d_pass2_img").src=li_ok;
}else{
obj.className="d_err";
obj.innerHTML=msg[3];
document.getElementById("d_pass2_img").src=li_err;
}
returnchk;
}

functionchk_reg(){
varchk=true
if(document.getElementById("ys_name").value==''){chk=false;
d_name.className="d_err";
d_name.innerHTML=msg[1];
document.getElementById("d_uname_img").src=li_err;
}
if(!out_upwd1()){chk=false}
if(!out_upwd2()){chk=false}
if(!out_email()){chk=false}
if(chk){
document.getElementById('save_stat').innerHTML='<imgsrc="images/loading.gif"align="absmiddle"/>資料提交中……請稍候……'
document.getElementById('regbotton').disabled='disabled';
varusername=document.regform.ys_name.value;
varpassword=document.regform.ys_pass.value;
varrepassword=document.regform.ys_pass2.value;
varemail=document.regform.ys_email.value;
//varurl2="reg.asp?action=save&ys_name=+"escape(username)"+&password=+"escape(username)"+&email="+escape(email);
//varurl2="reg.asp?action=save&username="+escape(username)+"&password="+escape(password)+"&email="+escape(email);
xmlHttp.open("POST","saveuser.asp",true);
xmlHttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlHttp.onreadystatechange=saveuser;
xmlHttp.send("username="+escape(username)+"&password="+escape(password)+"&email="+escape(email));
}
}

functionsaveuser(){
if(xmlHttp.readyState<4){
d_name.innerHTML="loading...";
}
if(4==xmlHttp.readyState){
if(200==xmlHttp.status){
vardate=xmlHttp.responseText;
if(date==1){
alert("使用者註冊成功!");
//window.location.href='\/exhibit';
window.location.href="reg.asp";
}else{
alert("註冊發生錯誤!");
window.location.href="reg.asp";
}
}else{
alert("error");
}
}

}

functioncallServer(){
varu_name=document.getElementById("ys_name").value;
if((u_name==null)||(u_name=="")){
d_name.className="d_err";
d_name.innerHTML=msg[1];
document.getElementById("d_uname_img").src=li_err;
chk=false
returnchk;};
xmlHttp.open("POST","checkuser.asp",true);
xmlHttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlHttp.onreadystatechange=updatePage;
xmlHttp.send("username="+escape(u_name));
}

functionupdatePage(){
if(xmlHttp.readyState<4){
d_name.innerHTML="loading...";
}
if(xmlHttp.readyState==4&&200==xmlHttp.status){
varresponse=xmlHttp.responseText;
if(response==1)
{
d_name.className="d_ok";
d_name.innerHTML='恭喜使用者名稱可以使用。';
document.getElementById("d_uname_img").src=li_ok;
}
else{
d_name.className="d_err";
document.getElementById("d_uname_img").src=li_err;
d_name.innerHTML="使用者名稱已佔用,請重新註冊";
}
}
}
-->
</script>
</head>

<body>
<!--#includefile="../top.asp"-->
<tablewidth="857"align="center"cellpadding="0"cellspacing="0">
<tr>
<tdheight="20"align="left"valign="top">&nbsp;</td>
</tr>
<tr>
<tdwidth="260"height="20"align="left"valign="top"><imgsrc="images/zc-top.jpg"width="857"height="20"/></td>
</tr>
<tr>
<tdheight="20"align="left"valign="top"><tablewidth="100%"cellspacing="0"cellpadding="0">
<tr>
<tdwidth="3"bgcolor="FF7300"></td>
<tdalign="center"><formid="regform"name="regform"method="post"action="reg.asp">
<tablewidth="90%"cellspacing="0"cellpadding="0">
<tr>
<tdheight="15"colspan="4"></td>
</tr>
<tr>
<tdheight="35"colspan="4"align="left"class="blut_title">請填寫註冊資訊:</td>
</tr>
<tr>
<tdwidth="12%"align="left"><imgsrc="images/button.jpg"width="11"height="11"id="d_email_img"/>&nbsp;Email:</td>
<tdwidth="30%"height="30"align="left"><inputname="ys_email"type="text"id="ys_email"size="30"onFocus="on_input('d_email')"onBlur="out_email()"/></td>
<tdwidth="42%"align="left"colspan="2"class="yellow12"id="d_email">推薦使用:·
網易
郵箱 ·QQ郵箱 ·SOHU郵箱</td>
</tr>
<tr>
<tdalign="left"><imgsrc="images/button.jpg"width="11"height="11"id="d_uname_img"/>&nbsp;會員名:</td>
<tdheight="30"align="left"><inputname="ys_name"type="text"id="ys_name"size="30"onFocus="on_input('d_name')"onBlur="callServer()"/></td>
<tdalign="left"class="z666_unlink"id="d_name"colspan="2">須以字母開頭,至少4位</td>
</tr>
<tr>
<tdalign="left"><imgsrc="images/button.jpg"width="11"height="11"id="d_pass_img"/>&nbsp;密  碼:</td>
<tdheight="30"align="left"><inputname="ys_pass"type="password"id="ys_pass"size="30"onFocus="on_input('d_pass')"onBlur="out_upwd1()"/></td>
<tdcolspan="2"align="left"class="z666_unlink"id="d_pass">密碼設定至少4位,請區分大小寫</td>
</tr>
<tr>
<tdalign="left"><imgsrc="images/button.jpg"width="11"height="11"id="d_pass2_img"/>&nbsp;重複密碼:</td>
<tdheight="30"align="left"><inputname="ys_pass2"type="password"id="ys_pass2"size="30"onFocus="on_input('d_pass2')"onBlur="out_upwd2()"/></td>
<tdcolspan="2"align="left"class="z666_unlink"id="d_pass2"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<tdheight="1"colspan="4"background="images/dian.jpg"></td>
</tr>
<tr>
<tdheight="46"colspan="4"align="center">
<label>
<!--<inputtype="image"name="regbotton"src="images/reg.jpg"onclick="chk_reg();"/>-->
<inputtype="button"name="submit"value="OK!確認提交註冊資訊"onclick="chk_reg();"id="regbotton">
</label><divid="save_stat"></div></td>
</tr>
</table>
</form>
</td>
<tdwidth="3"bgcolor="FF7300"></td>
</tr>
</table></td>
</tr>
<tr>
<tdheight="20"align="left"valign="top"><imgsrc="images/zc-bottom.jpg"width="857"height="11"/></td>
</tr>
</table>
<!--#includefile="../bottom.asp"-->
</body>
</html>
===================
zp.css

/*CSSDocument*/

body{font-family:"宋體";font-size:12px;color:#333333;line-height:20px;}

a:link{color:#333333;text-decoration:none;}
a:visited,a:active{text-decoration:none;color:#333333;}
a:hover{text-decoration:underline;color:#003278;}

a.blue12:link{color:#001D4C;text-decoration:none;}
a.blue12:visited,a.blue12:active{text-decoration:none;color:#001D4C;}
a.blue12:hover{text-decoration:underline;color:#FF6600;}


a.black14:link{color:#333333;text-decoration:none;font-size:14px;font-weight:bold;line-height:24px;}
a.black14:visited,a.black14:active{text-decoration:none;font-size:14px;font-weight:bold;color:#333333;line-height:24px;}
a.black14:hover{text-decoration:underline;color:#FF6600;font-size:14px;font-weight:bold;line-height:24px;}



a.title_kh:link,a.title_kh:visited{color:#333333;text-decoration:none;}
a.title_kh:hover{text-decoration:none;color:#FF6600;font-size:120%;}
a.title_kh:active{text-decoration:none;color:#FF6600;}

a.yellow:link,a.yellow:visited{color:#FF6600;text-decoration:none;}
a.yellow:hover{text-decoration:underline;color:#003278;}
a.yellow:active{text-decoration:none;color:#FF6600;}


a.tj:link,a.tj:visited{color:#FF0000;text-decoration:none;}
a.tj:hover{text-decoration:underline;color:#003278;}
a.tj:active{text-decoration:none;color:#003278;}


a.blue14:link,a.blue14:visited{color:#001D4C;text-decoration:none;font-weight:bold;font-size:14px}
a.blue14:hover{text-decoration:none;color:#FF6600;font-weight:bold;font-size:14px}
a.blue14:active{text-decoration:none;color:#001D4C;font-weight:bold;font-size:14px}

.img{border:1pxsolid#cccccc;}
.zt_table{border:1pxsolid#DDDBDB;}

.zt_title{color:#FF6600;font-weight:bold;font-size:14px;}
.blut_title{color:#003278;font-weight:bold;font-size:14px;}
.blut12_unlink{color:#003399;font-weight:bold;}

.blue{color:#003278;}
.yellow12{color:#FF3300;}
.yellow14{color:#FF3300;font-size:14px;}

.end_title{font-size:24px;font-family:"黑體";color:#003278;}
.end_zw{font-size:14px;line-height:24px;}
.z666_unlink{color:#666666;}


.wywg_nolink{color:#FF6600;font-weight:bold;font-size:14px;}
a.wywg:link,a.wywg:visited{color:#FF6600;text-decoration:underline;font-weight:bold;font-size:14px;}
a.wywg:hover{text-decoration:underline;color:#FF7F12;font-weight:bold;font-size:14px;}
a.wywg:active{text-decoration:underline;color:#FF6600;font-weight:bold;font-size:14px;}

a.zc:link,a.zc:visited{color:#FF6600;text-decoration:underline;}
a.zc:hover{text-decoration:underline;color:#FF7F12;}
a.zc:active{text-decoration:underline;color:#FF6600;}

.d_default{
padding:2px02px4px;
border:1pxsolid#f7f7f7;
}
.d_on{
padding:2px02px4px;
border:1pxsolid#FFCC00;
color:#000;
background-color:#FFFFDD;
}
.d_ok{
padding:2px02px24px;
border:1pxsolid#00BE00;
color:#000;
background:#E6FFE6url(li_ok.gif)no-repeat4px;
}
.d_err{
border:1pxsolid#F30;
color:#F00;
padding:2px02px24px;
background:#FFF1E4url(li_err.gif)no-repeat4px;
}
========================
checkuser.asp:
===========================
<!--#includefile="../inc/conn.asp"-->
<%
Response.ContentType="text/html"
Response.Charset="gb2312"
ys_name=trim(request("username"))
setrs=server.createobject("adodb.recordset")
sql="selectys_name,idfromys_imemberwhereys_name='"&ys_name&"'"
rs.opensql,conn,1,1
ifrs.bofandrs.eofthen
response.write"1"
else
response.write"0"
endif
rs.close
setrs=nothing
%>
saveuser.asp:

<!--#includefile="../inc/conn.asp"-->
<%ys_name=request("username")
ys_pass=request("password")
ys_email=request("email")
setjrs=server.CreateObject("adodb.recordset")
jsql="selectys_name,idfromys_imemberwhereys_name='"&ys_name&"'"
jrs.openjsql,conn,1,1
ifjrs.eofandjrs.bofthen
setrs=server.CreateObject("adodb.recordset")
sql="select*fromys_imemberwhereidisnull"
rs.opensql,conn,1,3
rs.addnew
rs("ys_name")=ys_name
rs("ys_pass")=ys_pass
rs("ys_email")=ys_email
rs.update
Session("ys_userame")=ys_name
rs.close
setrs=nothing
response.write"1"
else
response.write"0"
endif
jrs.close
setjrs=nothing
%>
http://www.corange.cn/archives/2008/03/456.html