咨询电话:186 7916 6165 咨询电话:186 7916 6165 (微信同号)    在线QQ:181796286
NEWS BLOG ·
学无止境
关注开优网络 关注前沿
ASP.NET在后台直接验证表单数据
ASP.NET获取DropDownList和CheckBoxList的值

ASP.NET使用ajax异步检测用户名方法

发表日期:2019-01-05    文章编辑:    浏览次数:3603    标签:

1、导入jQuery.js代码
<asp:TextBox ID="txtname" onblur="checkUsername(this.value);" class="txt" runat="server"></asp:TextBox><br />
<span id="mes_username" style="margin-left: 60px;">
//验证用户名
function checkUsername(username) {
            var username = $.trim(username);
            if (username.length == 0) {
                $("#mes_username").html("<img src='images/check_error.gif' align='absmiddle' />  <span style='color:red'>请输入用户名</span>");
                return;
            } else {
                var reg = /^[a-zA-Z0-9]{6,20}$/g;
                if (!reg.test(username)) {
                    $("#mes_username").html("<img src='images/check_error.gif' align='absmiddle' />  <span style='color:red'>用户名必须为6-20数字或英文的组合</span>");
                    return;
                } else {
                    var url = "handler/CheckUserName.ashx?username=" + username + "&t=" + new Date().valueOf(); //AJAX使用一般处理程序验证
                    $.get(url, function (data) {
                        if (data == "false") {
                            $("#mes_username").html("<img src='images/check_error.gif' align='absmiddle' />  <span style='color:red'>该用户名已存在</span>");
                            return;
                        } else {
                            $("#mes_username").html("<img src='images/access_allow.gif' align='absmiddle' />  <span style='color:red'></span>");
                            return;
                        }
                    });
                }
            }
        }    
//验证密码6-20个数字或英文字符
  function checkpwd(pwd) {
            var pwd = $.trim(pwd);
            if (pwd.length == 0) {
                $('#mes_pwd').html("<img src='images/wrong.png' />  <span style='color: Red;'>请输入密码</span>");
                return;
            }
            else {
                var reg = /^[a-zA-Z0-9]{6,20}$/g;
                if (!reg.test(pwd)) {
                    $('#mes_pwd').html("<img src='images/wrong.png' />  <span style='color: Red;'>密码长度必须大于6个字符小于20个字符</span>");
                    return;
                } else {
                    $('#mes_pwd').html("<img src='images/right.png' />");
                    return;
                }
            }
        }
//常规验证。弹出提示框的形式
<asp:Button ID="btnAdd" OnClientClick="return checkform();"  runat="server" Text="提交信息"  />
 function checkform() {
    var name = $.trim($('#<%=txtusername.ClientID %>').val());
    var pwd = $.trim($('#<%=txtpwd.ClientID %>').val());
    var cpwd = $.trim($('#<%=txtcpwd.ClientID %>').val());
    var question = $.trim($('#<%=txtquestion.ClientID %>').val());
    var answer = $.trim($('#<%=txtanswer.ClientID %>').val());
    var email = $.trim($('#<%=txtemail.ClientID %>').val());
    if (name.length == 0 || pwd.length == 0 || cpwd.length == 0 || question.length == 0 || answer.length == 0 || email.length == 0) {
          alert("请输入完整信息再提交");
          return false;
    }
}

创建:CheckUsername.ashx一般处理程序
 string username = context.Request.QueryString["username"];
            if (string.IsNullOrEmpty(username))
            {
                context.Response.Write("false");
                context.Response.End();
            }
            if (new DAL.UserDAL().ExistsUserName(username))
            {
                context.Response.Write("false");
                context.Response.End();
            }
            else
            {
                context.Response.Write("true");
                context.Response.End();
            }