Ajax校验用户是否存在
需求:做一个ajax登录
主要技术点:jquery ajax以及blur事件
当用户名输入框失去焦点的时候就会触发blur事件,然后进行ajax请求,获得结果(true或者false),如果请求结果为true,就把用户名输入框图片替换成ok,并且输出文字:恭喜您, 这个帐号可以注册,否则就替换成图片no,并且输出文字:账号已存在
源代码:
前台:
1: <%@ Page Language="C#" MasterPageFile="~/Top_Down.master" AutoEventWireup="true" CodeFile="RegisterMember.aspx.cs"Inherits="Member_RegisterMember" Title="注册用户" %>
2:
3: <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
4:
5: <link href="../Admin/css/template.css" rel="stylesheet" type="text/css" />
6:
7: <link href="../Admin/css/validationEngine.jquery.css" rel="stylesheet" type="text/css" />
8:
9: <script src="../Admin/scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
10:
11: <script src="../js/jquery.validationEngine.js" type="text/javascript"></script>
12:
13: <script src="../Admin/scripts/isValid.js" type="text/javascript"></script>
14:
15: <script src="../js/languages/jquery.validationEngine-zh_CN.js" type="text/javascript"></script>
16:
17: <script type="text/javascript">
18:
19: var IsCheck=false;
20:
21: $(function(){
22:
23: // binds form submission and fields to the validation engine
24:
25: $("#form1").validationEngine();
26:
27: //当鼠标失去焦点的时候验证
28:
29: $("#txtUserName").blur(function(){
30:
31: $.ajax({
32:
33: url:"Data/GetMemberInfo.ashx?method=CheckExistUserName",
34:
35: data:{"username":$("#txtUserName").val()},
36:
37: type:"post",
38:
39: success:function(text){
40:
41: $("#tdUser").empty();//清空内容
42:
43: var item;
44:
45: if(text=="True"){
46:
47: item='<img src="../images/ok.png"/>恭喜您,这个帐号可以注册!';
48:
49: IsCheck=true;
50:
51: }
52:
53: else
54:
55: item='<img src="../images/no.png"/>对不起,这个帐号已经有人注册了!';
56:
57: $("#tdUser").append(item);
58:
59: }
60:
61: });
62:
63: });
64:
65: });
66:
67: function CheckForm1()
68:
69: {
70:
71: if(IsCheck)
72:
73: {
74:
75: form1.submit();
76:
77: }
78:
79: else{
80:
81: alert("请验证用户名");
82:
83: }
84:
85: }
86:
87: </script>
88:
89: </asp:Content>
90:
91: <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
92:
93: <form id="form1" action="Data/GetMemberInfo.ashx?method=SaveMemberInfo" method="post">
94:
95: <div class="content">
96:
97: <div class="left_side">
98:
99: <div class="logo_bottom"></div>
100:
101: </div>
102:
103: <div class="right_side zhuce">
104:
105: <div class="zhuce_title"><p class="hide">注册新用户</p></div>
106:
107: <div class="zhuce_p">
108:
109: <table width="578" class="zc_table1">
110:
111: <tr>
112:
113: <td width="93" class="zc_tar">用户名:</td>
114:
115: <td width="200" class="zc_tal"><input type="text" class="zc_input1 validate[required,custom[LoginName]] text-input"name="txtUserName" id="txtUserName"/><!--LoginName-->
116:
117: </td>
118:
119: <td width="269" class="zc_font" id="tdUser"></td>
120:
121: </tr>
122:
123: <tr>
124:
125: <td class="zc_tar">密码:</td>
126:
127: <td class="zc_tal"><input type="password" class="zc_input2 validate[required,custom[LoginPwd]] text-input" id="txtPwd"name="txtPwd"/></td>
128:
129: <td class="zc_font"></td>
130:
131: </tr>
132:
133: <tr>
134:
135: <td class="zc_tar">确认密码:</td>
136:
137: <td class="zc_tal"><input type="password" class="zc_input3 validate[required,equals[txtPwd] text-input" /></td>
138:
139: <td class="zc_font"></td>
140:
141: </tr>
142:
143: <tr>
144:
145: <td class="zc_tar">E-mail:</td>
146:
147: <td class="zc_tal"><input type="text" class="zc_input4 validate[required,custom[email] text-input" name="txtEmail"id="txtEmail"/></td>
148:
149: <td class="zc_font"></td>
150:
151: </tr>
152:
153: <tr>
154:
155: <td class="zc_tar">验证码:</td>
156:
157: <td class="zc_tal" colspan="2"><input type="text" class="zc_input5" name="txtCheckCode" id="txtCheckCode"/><imgsrc="../Admin/FileManage/VerifyChars.ashx" alt="验证码" /></td>
158:
159: </tr>
160:
161: <tr><td> </td></tr>
162:
163: <tr>
164:
165: <td colspan="3" align="center"><a href="javascript:CheckForm1()"><img src="../images/zhuce_sumbit.png" /></a></td>
166:
167: </tr>
168:
169: </table>
170:
171: </div>
172:
173: </div>
174:
175: </div>
176:
177: </form>
178:
179: </asp:Content>
180:
后台事件:
1: <%@ WebHandler Language="C#" Class="GetMemberInfo" %>
2:
3: using System;
4:
5: using System.Web;
6:
7: using Common;
8:
9: using czcraft.Model;
10:
11: using czcraft.BLL;
12:
13: using System.Web.SessionState;
14:
15: public class GetMemberInfo : IHttpHandler,IRequiresSessionState
16:
17: {
18:
19: // //记录日志
20:
21: private static readonly log4net.ILog logger =log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
22:
23: public void ProcessRequest(HttpContext context)
24:
25: {
26:
27: String methodName = context.Request["method"];
28:
29: if (!string.IsNullOrEmpty(methodName))
30:
31: CallMethod(methodName, context);
32:
33: }
34:
35: /// <summary>
36:
37: /// 根据业务需求调用不同的方法
38:
39: /// </summary>
40:
41: /// <param name="Method">方法</param>
42:
43: /// <param name="context">上下文</param>
44:
45: public void CallMethod(string Method, HttpContext context)
46:
47: {
48:
49: switch (Method)
50:
51: {
52:
53: case "CheckExistUserName":
54:
55: CheckExistUserName(context);
56:
57: break;
58:
59: //case "SearchMember":
60:
61: // SearchMember(context);
62:
63: // break;
64:
65: case "SaveMemberInfo":
66:
67: SaveMemberInfo(context);
68:
69: break;
70:
71: //case "RemoveMember":
72:
73: // RemoveMember(context);
74:
75: // break;
76:
77: //case "GetMember":
78:
79: // GetMember(context);
80:
81: // break;
82:
83: default:
84:
85: return;
86:
87: }
88:
89: }
90:
91: /// <summary>
92:
93: /// 验证帐号是否存在
94:
95: /// </summary>
96:
97: /// <param name="context"></param>
98:
99: public void CheckExistUserName(HttpContext context)
100:
101: {
102:
103: string username = context.Request["username"];
104:
105: if (Tools.IsValidInput(ref username, true))
106:
107: {
108:
109: context.Response.Write(new memberBLL().CheckExistUserName(username));
110:
111: }
112:
113: }
114:
115: /// <summary>
116:
117: /// 保存用户信息
118:
119: /// </summary>
120:
121: /// <param name="context"></param>
122:
123: public void SaveMemberInfo(HttpContext context)
124:
125: {
126:
127: try
128:
129: {
130:
131: //表单读取
132:
133: string txtUserName = context.Request["txtUserName"];
134:
135: string txtPwd = context.Request["txtPwd"];
136:
137: string txtEmail = context.Request["txtEmail"];
138:
139: string txtCheckCode = context.Request["txtCheckCode"];
140:
141: //验证码校验
142:
143: if (!txtCheckCode.Equals(context.Session["checkcode"].ToString()))
144:
145: {
146:
147: return;
148:
149: }
150:
151: //字符串sql注入检测
152:
153: if (Tools.IsValidInput(ref txtUserName, true) && Tools.IsValidInput(ref txtPwd, true) && Tools.IsValidInput(ref txtEmail, true))
154:
155: {
156:
157: member info = new member();
158:
159: info.username = txtUserName;
160:
161: info.password = txtPwd;
162:
163: info.Email = txtEmail;
164:
165: info.states = "0";
166:
167: if (new memberBLL().AddNew(info) > 0)
168:
169: {
170:
171: SMTP smtp = new SMTP(info.Email);
172:
173: string webpath = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + "/Default.aspx";
174:
175: smtp.Activation(webpath, info.username);//发送激活邮件
176:
177: JScript.AlertAndRedirect("注册用户成功!!", "../Default.aspx");
178:
179: }
180:
181: else {
182:
183: JScript.AlertAndRedirect("注册用户失败!", "../Default.aspx");
184:
185: }
186:
187: }
188:
189: }
190:
191: catch (Exception ex)
192:
193: {
194:
195: logger.Error("错误!", ex);
196:
197: }
198:
199: }
200:
201: public bool IsReusable {
202:
203: get {
204:
205: return false;
206:
207: }
208:
209: }
210:
211: }
212:
效果截图: