注册页面:register.htm <script language="JavaScript"> function GetResult(str) { var exitdos = new ActiveXObject("Microsoft.XMLHTTP"); exitdos.open("POST","check.asp?userid="+escape(str),false); exitdos.send(); document.all.username.value=unescape(exitdos.responseText) } </script> userid:<input type="text" name="userid" onblur="GetResult(document.all.userid.value)"><br> username:<input type="text" name="username">
验证页面:check.asp <%@Language="JavaScript" function OpenDB(sdbname) { var connstr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(sdbname); var conn = Server.CreateObject("ADODB.Connection"); conn.Open(connstr); return conn; } var sResult = ""; var oConn = OpenDB("data.mdb"); var userid = unescape(Request("userid")); var sql = "select username from users where userid='"+userid+"'"; var rs = oConn.Execute(sql); if(!rs.EOF) { sResult = "该用户名已经存在!" } else { sResult = "该用户名可以注册!" } Response.Write escape(sResult)%>
mdb数据库名:data.mdb 表:users 表结构:id(自动编号)、userid(文本)、username(文本)
到此,源代码和数据库就完成了,因为用的是onblur事件,当光标的焦点离开时,input值会马上传到数据库验证。 |