废话不多说了,直接给大家贴代码了,具体代码如下所示:
<script type="text/javascript"> $(function () { $("#txtemail").blur(function () { $.ajax({ type: "post", url: "reg.ashx?email=" + $.trim($("#txtemail").val()) + "&d=" + (+new date()), success: function (data) { var vcount = parseint(data); if (vcount == 0) { alert("邮箱可以使用"); } else { alert("邮箱已经被占用"); } } }); }); $("#checkpwd").blur(function () { return checkpwd(); }); }); function checkpwd() { var bcheck = true; if ($.trim($("#pwd").val()) != $.trim($("#checkpwd").val())) { alert("两次密码输入不一致"); bcheck = false; } return bcheck; } </script>
reg.ashx代码:
using system; using system.collections.generic; using system.linq; using system.web; namespace webt1.ti.html2 { /// <summary> /// reg 的摘要说明 /// </summary> public class reg : ihttphandler { public void processrequest(httpcontext context) { if (context.request["email"] != null) { string stremail = context.request["email"]; list<usermodel> lstuser = dataservice.getuserlist(); var v = lstuser.where(p => p.email == stremail); int icount = 0; if (v.count() > 0) { icount = 1; } context.response.contenttype = "text/plain"; context.response.write(icount.tostring()); } } public bool isreusable { get { return false; } } } public class dataservice { /// <summary> /// 模拟已注册用户数据 /// </summary> public static list<usermodel> getuserlist() { var list = new list<usermodel>(); list.add(new usermodel() { email = "t1@demo.com" }); list.add(new usermodel() { email = "t2@demo.com" }); list.add(new usermodel() { email = "t3@demo.com" }); list.add(new usermodel() { email = "t4@demo.com" }); list.add(new usermodel() { email = "t5@demo.com" }); return list; } } public class usermodel { public string email { get; set; } } }
总结
以上所述是小编给大家介绍的ajax邮箱、用户名唯一性验证实例代码,希望对大家有所帮助