<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="ajax.js" type="text/javascript"></script>
</head>
<body onload="form1.u_name.focus();">
<form name="form1">
用户名: <input type="text" id="u_name" onChange="callServer();" />
<span id="test1"></span><br>
密 码:
<input type="password" />
<input type="submit" name="submit" value="注册" />
</form>
</body>
</html>
// JavaScript Document
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 = "userCheck.asp?name=" + escape(u_name);//查询地址 返回值为"已被注册"或"可以注册"
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
}
function updatePage() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200){
var response = xmlHttp.responseText;
if (response == "true"){
test1.innerHTML="";
}
else{
test1.innerHTML = "用户名不存在,请重新输入!";
document.getElementById("u_name").value="";
document.getElementById("u_name").focus();
}
}
else if(xmlHttp.status==500){
test1.innerHTML=xmlHttp.responseText;
}
}
}
[code="userCheck.asp文件"]
<%@LANGUAGE="VBSCRIPT"%>
<%
username = request.querystring("name")
if (username = "ajax") then
response.write "true"
else
response.write "false"
end if
%>
[/code]