无刷新判断用户名是否存在
两个页面:
Default.aspx 输入username去验证
checkUser.aspx 处理验证信息
Default页面代码
<!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 runat="server">
<title>无刷新判断用户名是否存在</title>
<script type="text/javascript" language="javascript">
var xmlHttp=null;
function CheckUser()
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(err1)
{
try
{
xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");
}
catch(err2)
{
xmlHttp =new XMLHttpRequest();
}
}
var Text1=document.getElementById("Text1");
var url="checkUser.aspx?userid="+Text1 .value;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=OnHandler;
xmlHttp.send(null);
}
function OnHandler()
{
if (xmlHttp.readyState==4)
{
var isValid=xmlHttp.responseText;
var exists=document.getElementById("exsits");
exists.innerHTML=isValid.substring(0,4);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Only when the input text is "zhangkai" there will be exist.<br />
<input id="Text1" type="text" onblur="CheckUser()" />
<label id="exsits" style="color:Red; font-size:10px;"></label><br />
<button value="asd" visible="true" type="button" onclick="CheckUser()">Validate</button>
</div>
</form>
</body>
</html>
CheckUser页面代码
if (!Page.IsPostBack)
{
if (Request.QueryString["userid"] != null)
{
string getUserID = string.Empty;
getUserID = Request.QueryString["userid"].ToString();
if (getUserID.Equals("zhangkai"))
{
Response.Write("存在");
}
else
{
Response.Write("不存在");
}
}
}
关于 ActiveXObject函数请见上篇文章 ActiveXObject函数【转】

浙公网安备 33010602011771号