ajax用戶注冊代碼
2024-09-01 08:30:35
供稿:網友
終于實現了ajax用戶注冊系統.。注冊界面如上。
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> 用戶注冊 </TITLE>
</HEAD>
<style>
<!--
body,td {font-size:12px;}
input{
background-color: #ffffff;
color: #000000;
font-family: tahoma;
font-size: 12px; border: 1px #cccccc double}
-->
</style>
<BODY>
<script language=javascript>
<!--
function checkUserName()
{
username=this.form1.username.value;
var xmlhttp;
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new XMLHttpRequest();
}
catch (e)
{
}
}
}
if (username=="")
{
msg="用戶名不能為空";
var ch=document.getElementById("check1");
ch.innerHTML="<font color='#aaaaaa'>"+msg+"</font>";
return false;
}
//創建請求,并使用escape對username編碼,以避免亂碼
xmlhttp.open("get","checkuser.asp?username="+escape(username)+
"&t=" + new Date().getTime());
xmlhttp.onreadystatechange=function()
{
if(4==xmlhttp.readyState)
{
if(200==xmlhttp.status)
{
if (xmlhttp.responseText=="yes")
{
msg="此用戶名已經存在";
}
else
{
msg="輸入正確";
}
}
else
{
msg="網絡鏈接失敗";
}
var ch=document.getElementById("check1");
ch.innerHTML="<font color='#aaaaaa'>"+msg+"</font>";
}
}
xmlhttp.send(null);
return false;
}
//-->
</script>
<script language="javascript">
function checkpass1()
{
var Inform="form1";
var Inputname="password1";
var Form=Inform+"."
eval("Temp="+Form+Inputname+".value;");
//alert(Temp);
if(Temp==""){
msg="此項不能為空";
}
else
{
if(Temp.length<6||Temp.length>20)
{
msg="密碼必須在6~20個字符之間";
}
else
{
var Inputname1="password";
eval("Temp1="+Form+Inputname1+".value");
if (Temp!=Temp1)
{
eval(Form+Inputname+".value='';");
eval(Form+Inputname1+".value='';");
eval(Form+Inputname1+".focus();");
msg="兩次密碼不一樣!";
msg1="";
var ch1=document.getElementById("password2");
ch1.innerHTML="<font color='#aaaaaa'>"+msg1+"</font>";
}
else
{
msg="輸入正確";
}
}
}
var ch=document.getElementById("password3");
ch.innerHTML="<font color='#aaaaaa'>"+msg+"</font>";
}
function checkpass()
{
var Inform="form1";
var Inputname="password";
var Form=Inform+"."
eval("Temp="+Form+Inputname+".value;");
//alert(Temp);
if(Temp==""){
msg="此項不能為空";
}
else
{
if(Temp.length<6||Temp.length>20)
{
msg="密碼必須在6~20個字符之間";
}
else
{
msg="輸入正確";
}
}
var ch=document.getElementById("password2");
ch.innerHTML="<font color='#aaaaaa'>"+msg+"</font>";
}
</script>
<script language=javascript>
function sendreg()
{
if(this.form1.username.value=="")
{
alert('請輸入要注冊的用戶名');
this.form1.username.focus();
return false;
}
if(this.form1.password.value=="")
{
alert('請輸入密碼');
this.form1.password.focus();
return false;
}
else
{
if(this.form1.password.value.length<6
||this.form1.password.value.length>20)
{
alert('密碼長度必須在6~20個字符');
this.form1.password.focus();
return false;
}
}
if(this.form1.password1.value=="")
{
alert('請輸入密碼');
this.form1.password1.focus();
return false;
}
else
{
if(this.form1.password1.value.length<6
||this.form1.password1.value.length>20)
{
alert('密碼長度必須在6~20個字符');
this.form1.password1.focus();
return false;
}
}
if(this.form1.password.value!=this.form1.password1.value)
{
this.form1.password.value="";
this.form1.password1.value="";
this.form1.password.focus();
alert('兩次密碼不一樣,請重新輸入');
return false;
}
username=this.form1.username.value;
password=this.form1.password.value;
var xmlhttp;
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new XMLHttpRecordset();
}
catch (e)
{
}
}
}
//創建請求,并使用escape對username編碼,以避免亂碼
xmlhttp.onreadystatechange=function()
{
if(4==xmlhttp.readyState)
{
if(200==xmlhttp.status)
{
if (xmlhttp.responseText=="yes")
{
this.form1.username.value="";
this.form1.password.value="";
this.form1.password1.value="";
document.getElementById("check1").innerHTML="";
document.getElementById("password2").innerHTML="";
document.getElementById("password3").innerHTML="";
alert('注冊成功');
}
else
{
this.form1.password.value="";
this.form1.password1.value="";
alert('注冊失敗');
}
}
else
{
alert('網絡連接失敗');
}
}
}
//打開連接
xmlhttp.open("post","reg.asp",true)
xmlhttp.setRequestHeader('Content-type',
'application/x-www-form-urlencoded');
//發送數據
xmlhttp.send("username="+escape(username)+
"&password="+escape(password));
}
</script>
<div align="center">
<TABLE border=1 cellpadding=0 cellspacing=0 width=500>
<form name="form1" method="post">
<TR>
<TD colspan=2 align="center" height=25><b>用戶注冊</b></TD>
</TR>
<TR>
<TD align=right width=100>用戶名</TD>
<TD align=left><input type=text size=30 name="username"
onBlur="return checkUserName();"> <span id="check1">
</span></TD>
</TR>
<TR>
<TD align=right>密碼</TD>
<TD align=left><input type=password size=30
name="password" onBlur="checkpass();"
maxlength=20> <span id="password2"></span>
</TD>
</TR>
<TR>
<TD align=right>確認密碼</TD>
<TD align=left><input type=Password size=30
name="password1" onBlur="checkpass1();"
maxlength=20> <span id="password3"></span></TD>
</TR>
<TR>
<TD align="center" colspan=2>
<input type="button" value="注冊" onclick="return sendreg();"
maxlength=20> <input type="reset" value="重置"></TD>
</TR>
</form>
</TABLE>
</div>
</BODY>
</HTML>
checkuser.asp
<!--#include file="adoconn.asp"-->
<%
username=Replace(request.querystring("username"),"'","")
sql="select * from puser where y_username='"&username&"'"
rs.open sql,conn,1,1
if rs.eof then
response.write "no"
else
response.write "yes"
end if
rs.close
%>
源碼下載地址:http://www.aspprogram.cn/soft.asp?id=12