首页 > 代码库 > asp.net ajax+ashx局部验证

asp.net ajax+ashx局部验证

优化用户体检功能

前台页面:

<script type="text/javascript">
function createXHL() {
if (window.XMLHttpRequest)
return new XMLHttpRequest;
else if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
}
var xhr = "";
function checkmail() {
var mail = document.getElementById("txtEmail").value;
var url = "AjaxMail.ashx?mail=" + mail;
//创建xhr对象
xhr = createXHL();
//设置回调函数
xhr.onreadystatechange = ReadDo;
//初始化xhr对象
xhr.open("get", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//发送
xhr.send(null);
}
function ReadDo() {
if (xhr.readyState == 4 && xhr.status == 200) {
var result = xhr.responseText;
var msg = document.getElementById("ltmail");
if (result == "true")
msg.innerHTML = "邮箱已存在!";
else
msg.innerHTML = "邮箱可以使用!";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>欢迎注册</h2>

<p>邮&nbsp;&nbsp; 箱:&nbsp;&nbsp;</p>
<input id="txtEmail" type="text" class="input" onchange="checkmail()" /><br/>
<asp:Label ID="ltmail" runat="server" Text="请输入常用邮箱"></asp:Label>
</div>

</form>

</body>

</html>