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>
onblur="CheckUser()":用户名是否重复检测,当离开焦点时触发事件。
onblur="passcheck()":二次密码输入是否一致验证
3、jquery
<script language="javascript" type="text/javascript">
function passcheck() {
var pwd1 = document.getElementById("<%=password1.ClientID %>").value;
var pwd2 = document.getElementById("<%=password2.ClientID %>").value;
if(pwd1 == pwd2){
return true;
}
else
{
window.alert('两次输入密码不一致');
//document.getElementById("<%=Button1.ClientID %>").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('<span style ="color: red"> 已经存在,请重新输入!</span>');
$("#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;
}
}
本文暂时没有评论,来添加一个吧(●'◡'●)