1、引入JQUERY
<title>注册新用户title> <script src="js/jquery.min2.js" type="text/javascript">script>
2、html
<div class="layui-form-item"> <label for="L_username" class="layui-form-label"> 登录用户名label> <div class="layui-input-inline"> <asp:TextBox ID="username" runat="server" onblur="CheckUser()" autocomplete="off" class="layui-input">asp:TextBox> div> <div id="remind" class="layui-form-mid layui-word-aux"> div> div> <div class="layui-form-item"> <label for="L_pass" class="layui-form-label"> 密码label> <div class="layui-input-inline"> <asp:TextBox ID="password1" runat="server" TextMode="Password" autocomplete="off" class="layui-input">asp:TextBox> div> div> <div class="layui-form-item"> <label for="L_repass" class="layui-form-label"> 确认密码label> <div class="layui-input-inline"> <asp:TextBox ID="password2" runat="server" onblur="passcheck()" TextMode="Password" required="" autocomplete="off" class="layui-input">asp:TextBox> div> div>
οnblur="CheckUser()":用户名是否重复检测,当离开焦点时触发事件。
οnblur="passcheck()":二次密码输入是否一致验证
3、jquery
<script language="javascript" type="text/javascript"> function passcheck() { var pwd1 = document.getElementById("").value; var pwd2 = document.getElementById("").value; if(pwd1 == pwd2){ return true; } else { window.alert('两次输入密码不一致'); //document.getElementById("").disabled = true; $("#password1").focus(); return false; } } function CheckUser() { var userid = $("#username").val(); if (userid.length>0) { $.ajax({ type: 'get', url: 'reg_check.ashx', contentType: "application/json;charset=utf-8", dataType: "text", data: { username: userid }, success: function (data) { if (data == "True") { $("#remind").html(' 已经存在,请重新输入!'); $("#username").focus(); } else { $("#remind").html("用户名可用"); } }, error: function () { $("#remind").html("发生错误"); } }); } else { window.alert('用户名不能为空'); $("#username").focus(); } } </script>
用户名是否存在检测,如果不为空,则需要将数据发送到reg_check.ashx,根据返回的数据做出判断,否则提示不能为空。
4、reg_check.ashx
string username = context.Request.QueryString["username"];bool result = new DAL.admin().Exists(username);context.Response.Write(result);
这里面使用到DAL中的方法
//检测用户名是否存在 public bool Exists(string str) { StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from [admin]"); strSql.Append(" where [username]=@str"); SqlParameter[] parameters = { new SqlParameter("@str",SqlDbType.NVarChar,50)}; parameters[0].Value = str; object obj = new SqlHelper().ExecuteScalar(strSql.ToString(), parameters, CommandType.Text); int res = Convert.ToInt32(obj); if (res > 0) { return true; } else { return false; } }