用ajax与asp.net写一个注册验程序,验证用户邮箱在数据库中是否存在,就是输完之后,填写另一项信息前判断是否存在,不用按钮点击那种。。
------解决方案--------------------------------------------------------
//ajax
var name=document.getElementById("邮箱控件的ID").value;
$.ajax({
url: "ISName.ashx?name=" + name + "&t=" + new Date(),
type: "get",
success: function(msg) {
//msg:ISName.ashx的返回值
if (msg == "0") {
alert("恭喜您,此邮箱可用!");
}
else {
alert("此邮箱已经存在,请重新输入!");
}
}
})
//ISName.ashx
查询数据库判断邮箱是否存在
------解决方案--------------------------------------------------------
前台:
- HTML code
<div> <table style="width: 90%;"> <tr> <th colspan="3"> <strong>基本信息</strong> </th> </tr> <tr> <td style="width: 30%;"> <strong>电子邮箱:</strong> </td> <td style="width: 30%;"> <input id="email" name="email" value="" onblur="CheckEmail()" /> </td> <td id="errorEmailMsg" style="width: 30%;"> </td> </tr> <tr> <td style="width: 30%;"> <strong>姓名:</strong> </td> <td style="width: 30%;"> <input id="name" name="name" value="" /> </td> <td style="width: 30%;"> </td> </tr> </table> </div> <script language="javascript" type="text/javascript"> var CheckEmail = function () { var currentEmail = $("#email").val(); $.ajax({ url: "/Test/CheckEmail", dataType: "json", type: "post", data: "email=" + currentEmail, success: function (result) { if (result.isEmailExist == "1") { $("#errorEmailMsg")("<span style='color:red'>当前Email已经存在,请换一个。</span>"); } else { $("#errorEmailMsg")("<span style='color:green'>Email不存在测试成功</span>"); } } }) } </script>
------解决方案--------------------------------------------------------
正解~