ASP.NET使用ajax異步檢測用戶名方法
發(fā)表日期:2019-01-05 文章編輯: 瀏覽次數(shù):4025 標(biāo)簽:
1、導(dǎo)入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;">
//驗(yàn)證用戶名
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數(shù)字或英文的組合</span>");
return;
} else {
var url = "handler/CheckUserName.ashx?username=" + username + "&t=" + new Date().valueOf(); //AJAX使用一般處理程序驗(yàn)證
$.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;
}
});
}
}
}
//驗(yàn)證密碼6-20個(gè)數(shù)字或英文字符
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個(gè)字符小于20個(gè)字符</span>");
return;
} else {
$('#mes_pwd').html("<img src='images/right.png' />");
return;
}
}
}
//常規(guī)驗(yàn)證。彈出提示框的形式
<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;
}
}
創(chuàng)建: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();
}