asp.net+Ajax校驗(yàn)用戶(hù)是否存在的實(shí)現(xiàn)代碼
更新時(shí)間:2012年05月01日 17:56:20 作者:
主要技術(shù)點(diǎn) jquery ajax以及blur事件,當(dāng)用戶(hù)名輸入框失去焦點(diǎn)的時(shí)候就會(huì)觸發(fā)blur事件,然后進(jìn)行ajax請(qǐng)求,獲得結(jié)果(true或者false),如果請(qǐng)求結(jié)果為true,就把用戶(hù)名輸入框圖片替換成ok,并且輸出文字:恭喜您
需求:做一個(gè)ajax登錄
主要技術(shù)點(diǎn):jquery ajax以及blur事件
當(dāng)用戶(hù)名輸入框失去焦點(diǎn)的時(shí)候就會(huì)觸發(fā)blur事件,然后進(jìn)行ajax請(qǐng)求,獲得結(jié)果(true或者false),如果請(qǐng)求結(jié)果為true,就把用戶(hù)名輸入框圖片替換成ok,并且輸出文字:恭喜您, 這個(gè)帳號(hào)可以注冊(cè),否則就替換成圖片no,并且輸出文字:賬號(hào)已存在
源代碼:
前臺(tái):
<%@ Page Language="C#" MasterPageFile="~/Top_Down.master" AutoEventWireup="true" CodeFile="RegisterMember.aspx.cs"Inherits="Member_RegisterMember" Title="注冊(cè)用戶(hù)" %>
<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();
//當(dāng)鼠標(biāo)失去焦點(diǎn)的時(shí)候驗(yàn)證
$("#txtUserName").blur(function(){
$.ajax({
url:"Data/GetMemberInfo.ashx?method=CheckExistUserName",
data:{"username":$("#txtUserName").val()},
type:"post",
success:function(text){
$("#tdUser").empty();//清空內(nèi)容
var item;
if(text=="True"){
item='<img src="../images/ok.png"/>恭喜您,這個(gè)帳號(hào)可以注冊(cè)!';
IsCheck=true;
}
else
item='<img src="../images/no.png"/>對(duì)不起,這個(gè)帳號(hào)已經(jīng)有人注冊(cè)了!';
$("#tdUser").append(item);
}
});
});
});
function CheckForm1()
{
if(IsCheck)
{
form1.submit();
}
else{
alert("請(qǐng)驗(yàn)證用戶(hù)名");
}
}
</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">注冊(cè)新用戶(hù)</p></div>
<div class="zhuce_p">
<table width="578" class="zc_table1">
<tr>
<td width="93" class="zc_tar">用戶(hù)名:</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">確認(rèn)密碼:</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">驗(yàn)證碼:</td>
<td class="zc_tal" colspan="2"><input type="text" class="zc_input5" name="txtCheckCode" id="txtCheckCode"/><imgsrc="../Admin/FileManage/VerifyChars.ashx" alt="驗(yàn)證碼" /></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>
后臺(tái)事件:
<%@ 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>
/// 根據(jù)業(yè)務(wù)需求調(diào)用不同的方法
/// </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>
/// 驗(yàn)證帳號(hào)是否存在
/// </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>
/// 保存用戶(hù)信息
/// </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"];
//驗(yàn)證碼校驗(yàn)
if (!txtCheckCode.Equals(context.Session["checkcode"].ToString()))
{
return;
}
//字符串sql注入檢測(cè)
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);//發(fā)送激活郵件
JScript.AlertAndRedirect("注冊(cè)用戶(hù)成功!!", "../Default.aspx");
}
else {
JScript.AlertAndRedirect("注冊(cè)用戶(hù)失敗!", "../Default.aspx");
}
}
}
catch (Exception ex)
{
logger.Error("錯(cuò)誤!", ex);
}
}
public bool IsReusable {
get {
return false;
}
}
}
主要技術(shù)點(diǎn):jquery ajax以及blur事件
當(dāng)用戶(hù)名輸入框失去焦點(diǎn)的時(shí)候就會(huì)觸發(fā)blur事件,然后進(jìn)行ajax請(qǐng)求,獲得結(jié)果(true或者false),如果請(qǐng)求結(jié)果為true,就把用戶(hù)名輸入框圖片替換成ok,并且輸出文字:恭喜您, 這個(gè)帳號(hào)可以注冊(cè),否則就替換成圖片no,并且輸出文字:賬號(hào)已存在
源代碼:
前臺(tái):
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" MasterPageFile="~/Top_Down.master" AutoEventWireup="true" CodeFile="RegisterMember.aspx.cs"Inherits="Member_RegisterMember" Title="注冊(cè)用戶(hù)" %>
<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();
//當(dāng)鼠標(biāo)失去焦點(diǎn)的時(shí)候驗(yàn)證
$("#txtUserName").blur(function(){
$.ajax({
url:"Data/GetMemberInfo.ashx?method=CheckExistUserName",
data:{"username":$("#txtUserName").val()},
type:"post",
success:function(text){
$("#tdUser").empty();//清空內(nèi)容
var item;
if(text=="True"){
item='<img src="../images/ok.png"/>恭喜您,這個(gè)帳號(hào)可以注冊(cè)!';
IsCheck=true;
}
else
item='<img src="../images/no.png"/>對(duì)不起,這個(gè)帳號(hào)已經(jīng)有人注冊(cè)了!';
$("#tdUser").append(item);
}
});
});
});
function CheckForm1()
{
if(IsCheck)
{
form1.submit();
}
else{
alert("請(qǐng)驗(yàn)證用戶(hù)名");
}
}
</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">注冊(cè)新用戶(hù)</p></div>
<div class="zhuce_p">
<table width="578" class="zc_table1">
<tr>
<td width="93" class="zc_tar">用戶(hù)名:</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">確認(rèn)密碼:</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">驗(yàn)證碼:</td>
<td class="zc_tal" colspan="2"><input type="text" class="zc_input5" name="txtCheckCode" id="txtCheckCode"/><imgsrc="../Admin/FileManage/VerifyChars.ashx" alt="驗(yàn)證碼" /></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>
后臺(tái)事件:
復(fù)制代碼 代碼如下:
<%@ 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>
/// 根據(jù)業(yè)務(wù)需求調(diào)用不同的方法
/// </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>
/// 驗(yàn)證帳號(hào)是否存在
/// </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>
/// 保存用戶(hù)信息
/// </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"];
//驗(yàn)證碼校驗(yàn)
if (!txtCheckCode.Equals(context.Session["checkcode"].ToString()))
{
return;
}
//字符串sql注入檢測(cè)
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);//發(fā)送激活郵件
JScript.AlertAndRedirect("注冊(cè)用戶(hù)成功!!", "../Default.aspx");
}
else {
JScript.AlertAndRedirect("注冊(cè)用戶(hù)失敗!", "../Default.aspx");
}
}
}
catch (Exception ex)
{
logger.Error("錯(cuò)誤!", ex);
}
}
public bool IsReusable {
get {
return false;
}
}
}
相關(guān)文章
ASP.NET 程序中刪除文件夾導(dǎo)致session失效問(wèn)題的解決辦法分享
這篇文章主要介紹了ASP.NET 程序中刪除文件夾導(dǎo)致session失效問(wèn)題的解決辦法分享,有需要的朋友可以參考一下2013-12-12異步調(diào)用webservice返回responseXML為空的問(wèn)題解決方法
異步調(diào)用webservice返回responseXML為空,詳細(xì)很多朋友都遇到過(guò)類(lèi)似的問(wèn)題吧,接下來(lái)為大家提供詳細(xì)的解決方案,感興趣的朋友可以參考下哈2013-04-04一個(gè)簡(jiǎn)單的ASP.NET Forms 身份認(rèn)證的實(shí)例方法
當(dāng)訪問(wèn)默認(rèn)首頁(yè)default.aspx時(shí),會(huì)自動(dòng)跳轉(zhuǎn)到login.aspx頁(yè)面上請(qǐng)求登錄,隨便輸入用戶(hù)名和密碼,點(diǎn)擊“登錄”按鈕,會(huì)回到首頁(yè),并顯示當(dāng)前登錄的用戶(hù)名。2013-07-07在.net中用CheckBoxList實(shí)現(xiàn)單選
用CheckBoxList實(shí)現(xiàn)單選的原因是我覺(jué)得CheckBoxList控件頁(yè)面展示效果要好看一些,需要的朋友可以參考下2014-02-02net core下鏈路追蹤skywalking安裝和簡(jiǎn)單使用教程
本文將從0開(kāi)始搭建兩個(gè)webapi項(xiàng)目,使用Skywalking來(lái)追蹤他們之間的調(diào)用關(guān)系及響應(yīng)時(shí)間,開(kāi)發(fā)環(huán)境為VisualStudio2019,對(duì)net core 鏈路追蹤skywalking安裝和使用教程感興趣的朋友一起看看吧2021-10-10Asp.net Core MVC中怎么把二級(jí)域名綁定到特定的控制器上
這篇文章主要介紹了Asp.net Core MVC中怎么把二級(jí)域名綁定到特定的控制器上,需要的朋友可以參考下2017-06-06.Net中的Junction Points(交接點(diǎn))操作
這篇文章介紹了.Net中的Junction Points(交接點(diǎn))操作,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06