程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net+Ajax校驗用戶是否存在的實現代碼

asp.net+Ajax校驗用戶是否存在的實現代碼

編輯:ASP.NET基礎
需求:做一個ajax登錄

主要技術點:jquery ajax以及blur事件

當用戶名輸入框失去焦點的時候就會觸發blur事件,然後進行ajax請求,獲得結果(true或者false),如果請求結果為true,就把用戶名輸入框圖片替換成ok,並且輸出文字:恭喜您, 這個帳號可以注冊,否則就替換成圖片no,並且輸出文字:賬號已存在

源代碼:

前台:
復制代碼 代碼如下:
<%@ Page Language="C#" MasterPageFile="~/Top_Down.master" AutoEventWireup="true" CodeFile="RegisterMember.aspx.cs"Inherits="Member_RegisterMember" Title="注冊用戶" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link href="../Admin/css/template.css" rel="stylesheet" type="text/css" />
<link href="../Admin/css/validationEngine.jquery.css" rel="stylesheet" type="text/css" />
<script src="../Admin/scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="../js/jquery.validationEngine.js" type="text/javascript"></script>
<script src="../Admin/scripts/isValid.js" type="text/javascript"></script>
<script src="../js/languages/jquery.validationEngine-zh_CN.js" type="text/javascript"></script>
<script type="text/javascript">
var IsCheck=false;
$(function(){
// binds form submission and fields to the validation engine
$("#form1").validationEngine();
//當鼠標失去焦點的時候驗證
$("#txtUserName").blur(function(){
$.ajax({
url:"Data/GetMemberInfo.ashx?method=CheckExistUserName",
data:{"username":$("#txtUserName").val()},
type:"post",
success:function(text){
$("#tdUser").empty();//清空內容
var item;
if(text=="True"){
item='<img src="../images/ok.png"/>恭喜您,這個帳號可以注冊!';
IsCheck=true;
}
else
item='<img src="../images/no.png"/>對不起,這個帳號已經有人注冊了!';
$("#tdUser").append(item);
}
});
});
});
function CheckForm1()
{
if(IsCheck)
{
form1.submit();
}
else{
alert("請驗證用戶名");
}
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<form id="form1" action="Data/GetMemberInfo.ashx?method=SaveMemberInfo" method="post">
<div class="content">
<div class="left_side">
<div class="logo_bottom"></div>
</div>
<div class="right_side zhuce">
<div class="zhuce_title"><p class="hide">注冊新用戶</p></div>
<div class="zhuce_p">
<table width="578" class="zc_table1">
<tr>
<td width="93" class="zc_tar">用戶名:</td>
<td width="200" class="zc_tal"><input type="text" class="zc_input1 validate[required,custom[LoginName]] text-input"name="txtUserName" id="txtUserName"/><!--LoginName-->
</td>
<td width="269" class="zc_font" id="tdUser"></td>
</tr>
<tr>
<td class="zc_tar">密碼:</td>
<td class="zc_tal"><input type="password" class="zc_input2 validate[required,custom[LoginPwd]] text-input" id="txtPwd"name="txtPwd"/></td>
<td class="zc_font"></td>
</tr>
<tr>
<td class="zc_tar">確認密碼:</td>
<td class="zc_tal"><input type="password" class="zc_input3 validate[required,equals[txtPwd] text-input" /></td>
<td class="zc_font"></td>
</tr>
<tr>
<td class="zc_tar">E-mail:</td>
<td class="zc_tal"><input type="text" class="zc_input4 validate[required,custom[email] text-input" name="txtEmail"id="txtEmail"/></td>
<td class="zc_font"></td>
</tr>
<tr>
<td class="zc_tar">驗證碼:</td>
<td class="zc_tal" colspan="2"><input type="text" class="zc_input5" name="txtCheckCode" id="txtCheckCode"/><imgsrc="../Admin/FileManage/VerifyChars.ashx" alt="驗證碼" /></td>
</tr>
<tr><td> </td></tr>
<tr>
<td colspan="3" align="center"><a href="javascript:CheckForm1()"><img src="../images/zhuce_sumbit.png" /></a></td>
</tr>
</table>
</div>
</div>
</div>
</form>
</asp:Content>

後台事件:
復制代碼 代碼如下:
<%@ WebHandler Language="C#" Class="GetMemberInfo" %>
using System;
using System.Web;
using Common;
using czcraft.Model;
using czcraft.BLL;
using System.Web.SessionState;
public class GetMemberInfo : IHttpHandler,IRequiresSessionState
{
// //記錄日志
private static readonly log4net.ILog logger =log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void ProcessRequest(HttpContext context)
{
String methodName = context.Request["method"];
if (!string.IsNullOrEmpty(methodName))
CallMethod(methodName, context);
}
/// <summary>
/// 根據業務需求調用不同的方法
/// </summary>
/// <param name="Method">方法</param>
/// <param name="context">上下文</param>
public void CallMethod(string Method, HttpContext context)
{
switch (Method)
{
case "CheckExistUserName":
CheckExistUserName(context);
break;
//case "SearchMember":
// SearchMember(context);
// break;
case "SaveMemberInfo":
SaveMemberInfo(context);
break;
//case "RemoveMember":
// RemoveMember(context);
// break;
//case "GetMember":
// GetMember(context);
// break;
default:
return;
}
}
/// <summary>
/// 驗證帳號是否存在
/// </summary>
/// <param name="context"></param>
public void CheckExistUserName(HttpContext context)
{
string username = context.Request["username"];
if (Tools.IsValidInput(ref username, true))
{
context.Response.Write(new memberBLL().CheckExistUserName(username));
}
}
/// <summary>
/// 保存用戶信息
/// </summary>
/// <param name="context"></param>
public void SaveMemberInfo(HttpContext context)
{
try
{
//表單讀取
string txtUserName = context.Request["txtUserName"];
string txtPwd = context.Request["txtPwd"];
string txtEmail = context.Request["txtEmail"];
string txtCheckCode = context.Request["txtCheckCode"];
//驗證碼校驗
if (!txtCheckCode.Equals(context.Session["checkcode"].ToString()))
{
return;
}
//字符串sql注入檢測
if (Tools.IsValidInput(ref txtUserName, true) && Tools.IsValidInput(ref txtPwd, true) && Tools.IsValidInput(ref txtEmail, true))
{
member info = new member();
info.username = txtUserName;
info.password = txtPwd;
info.Email = txtEmail;
info.states = "0";
if (new memberBLL().AddNew(info) > 0)
{
SMTP smtp = new SMTP(info.Email);
string webpath = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + "/Default.aspx";
smtp.Activation(webpath, info.username);//發送激活郵件
JScript.AlertAndRedirect("注冊用戶成功!!", "../Default.aspx");
}
else {
JScript.AlertAndRedirect("注冊用戶失敗!", "../Default.aspx");
}
}
}
catch (Exception ex)
{
logger.Error("錯誤!", ex);
}
}
public bool IsReusable {
get {
return false;
}
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved