注册 登录
编程论坛 jQuery论坛

ajax验证输入数据代码

koka2009 发布于 2006-07-29 17:01, 1641 次点击
那位大哥有ajax验证注册数据验证的代码,给个参考一下吧。最好能实现异步传输,验证用户名是否存在。
5 回复
#2
hangxj2006-08-07 11:06

最近刚在网上看到一个,然后自己修改一下,还有很多不足的地方,自己改改吧,要是改得更好了,麻烦通知一下,让我也学一下

index.asp

[CODE]
<script language="javascript" type="text/javascript">
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
function callServer() {
var u_name = document.getElementById("u_name").value;
if ((u_name == null) || (u_name == "")) return;
var url = "cu.asp?name=" + escape(u_name);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
}
function updatePage() {
if (xmlHttp.readyState < 4) {
test1.innerHTML="loading...";
}
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
test1.innerHTML=response;
}
if (test1.innerHTML=="已被注册"){
document.form1.submit.disabled=true}
else{
document.form1.submit.disabled=false
}
}
</script>

<form name="form1" action="t1.asp" method="post" autocomplete="off">
用户名: <input type="text" name="u_name" id="u_name" onkeyup="callServer();" />
<span id="test1">是否能注册</span><br>
密 码:<input type="password" />
<input type="submit" name="submit" value="注册" />
</form>
<%Session.CodePage=936%>
[/CODE]

cu.asp

[CODE]<%
Response.Buffer = True
Response.ExpiresAbsolute = Now() - 1
Response.Expires = 0
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "No-Cache"
%>
<!--#include file="conn.asp"-->
<%
name=request.querystring("name")
if Len(name)<4 or Len(name)>20 then
response.write("长度不对")
else
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from u_ser where u_name='"&name&"'"
rs.Open sql,conn,1,1
if rs.eof and rs.bof then
response.write("可以注册")
else
response.write("已被注册")
end if
rs.close
set rs=nothing
end if
call CloseDatabase
%>[/CODE]

conn.asp
自己写吧!!

[此贴子已经被作者于2006-8-7 11:06:33编辑过]

#3
koka20092006-08-07 11:16
多谢,我自己再细心研究一下,有新发现一定相告。
#4
koka20092006-08-07 14:37

上面的call CloseDatabase是什么意思?关闭数据库连接的?还是其他用途。请指教

#5
hangxj2006-08-08 10:40

call CloseDatabase 是在CONN.ASP这个页面定义的,用来关闭数据库连接的.

CONN.ASP

[CODE]<%
on error resume next
db_path = "db1.mdb"
Set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath(db_path)
conn.Open connstr
Function CloseDatabase
Conn.close
Set conn = Nothing
End Function
%>[/CODE]

#6
hangxj2006-08-08 10:49
补充说明:
如果出现乱码的话,请在index.asp页面的前面加上
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
1