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