注册信息验证:
1 前台验证(js+Ajax):
首先像"邮箱格式","非空"验证可以通过js+正则表达式来完成;
另外像"用户名是否存在","邮箱是否已使用"可通过 Ajax向后台页面发请求,实现无页面刷新的验证()
2 仅仅是前台验证会存在一定的安全隐患,所以当用户提交时候还要实现后台验证
注意 验证"邮箱"的时候有一句
$.post("/ashx/ValidateReg.ashx", { "action": "mail", "userMail": val }, function (data) {
$("#msgEamil").css("display", "inline"); warnMsg("#msgEamil", data); });using BookShopManager.Web.Common;using System;using System.Collections.Generic;using System.Web;using System.Web.SessionState;namespace BookShopManager.Web.Ashx{ ////// ValidateReg 的摘要说明 /// 前台页面校验 /// public class ValidateReg : IHttpHandler, IRequiresSessionState { BLL.Users UserManager = new BLL.Users(); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string action = context.Request["action"]; if (action == "mail") { CheckedUserMail(context); } else if (action == "code") { string validateCode = context.Request["validateCode"]; if (ValidateCode.CheckValidateCode(validateCode)) { context.Response.Write("√"); } else { context.Response.Write("验证码错误"); } } else if (action == "name") { string loginName = context.Request["loginName"]; if (UserManager.ExistsByUserName(loginName)) { context.Response.Write("用户名已存在"); } else { context.Response.Write("√"); } } } #region 校验邮箱 private void CheckedUserMail(HttpContext context) { string userMail = context.Request["userMail"]; if (UserManager.ExistsByUserMail(userMail)) { context.Response.Write("邮箱已使用"); } else { context.Response.Write("√"); } } #endregion public bool IsReusable { get { return false; } } }}
另外当点击"注册"按钮后,将表单元素序列化为json数组对象
var par = $("#aspnetForm").serializeArray();
par = JSON.stringify(par);
$.post("/ashx/UserRegister.ashx", { "parameter": par }, function (data) { alert(data); });
需要在后台引用 命名空间来实现反序列化
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;JArray ja = (JArray)JsonConvert.DeserializeObject(jsonStr);//反序列化
using System;using System.Collections.Generic;using System.Web;using System.IO;using System.Runtime.Serialization.Formatters.Binary;using System.Text;using System.ServiceModel;using System.ServiceModel.Web;using System.Runtime.Serialization;using System.Runtime.Serialization.Json;using System.Web.Script.Serialization;using Newtonsoft.Json;using Newtonsoft.Json.Linq;using BookShopManager.Web.Common;using BookShopManager.BLL;using System.Web.SessionState;using BookShopManager.Web.Enum;using System.Web.UI;namespace BookShopManager.Web.Ashx{ ////// UserRegister 的摘要说明 /// 注册按钮后 进行"校验"和"新增" /// public class UserRegister : IHttpHandler, IRequiresSessionState { //"用户注册"服务端校验 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string strJson = context.Request["parameter"]; Model.Users userInfo; BLL.Users userManager = new BLL.Users(); string msg = string.Empty; if (ValidateUserInfo(strJson, out msg, out userInfo)) { //校验成功,进行添加 if (userManager.Add(userInfo) > 0) { string strScript = ""; HttpContext.Current.Session["vCode"] = null; context.Session["userInfo"] = userInfo; context.Response.Redirect(""); } else { //跳到错误页面 context.Response.Redirect("/ShowMsg.aspx?msg=" + "注册失败" + "&title=首页" + "&url=/Default.aspx"); } } else { context.Response.Write(msg); } } private bool ValidateUserInfo(string jsonStr,out string msg, out Model.Users UserInfo) { JArray ja = (JArray)JsonConvert.DeserializeObject(jsonStr); UserInfo = new Model.Users(); BLL.Users UserManager = new BLL.Users(); UserInfo.LoginId = ja[1]["value"].ToString(); //用户名 UserInfo.Name = ja[2]["value"].ToString(); //真实姓名 UserInfo.LoginPwd = ja[3]["value"].ToString(); //密码: string confirmPwd = ja[4]["value"].ToString(); //确认密码 UserInfo.Address = ja[6]["value"].ToString(); //地址: UserInfo.Phone = ja[7]["value"].ToString(); //手机: UserInfo.Mail = ja[5]["value"].ToString(); //Email: string validate = ja[8]["value"].ToString(); //验证码: msg = string.Empty; if (string.IsNullOrEmpty(UserInfo.LoginId)) { msg += "用户名不能为空!! \n"; } if (string.IsNullOrEmpty(UserInfo.Name)) { msg += "真实姓名不能为空!! \n"; } if (string.IsNullOrEmpty(UserInfo.LoginPwd)) { msg += "密码不能为空!!\n "; } if (string.IsNullOrEmpty(confirmPwd)) { msg += "确认密码不能为空!!\n "; } if (string.IsNullOrEmpty(UserInfo.Address)) { msg += "地址不能为空!!\n "; } if (string.IsNullOrEmpty(UserInfo.Phone)) { msg += "手机号不能为空!!\n "; } if (string.IsNullOrEmpty(UserInfo.Mail)) { msg += "邮箱不能为空!!\n "; } if (string.IsNullOrEmpty(validate)) { msg += "验证码不能为空!!\n "; } if (msg.Length > 1) { return false; } //判断密码是否一致 if (UserInfo.LoginPwd != confirmPwd) { msg += "两次密码输入不一致!! \n"; return false; } //校验验证码是否正确 if (!ValidateCode.CheckValidateCode(validate)) { msg += "验证码不正确!! \n"; return false; } //校验邮箱是否已使用, if (UserManager.ExistsByUserMail(UserInfo.Mail)) { msg += "邮箱已使用!! \n"; return false; } //用户名是否存在 if (UserManager.ExistsByUserName(UserInfo.LoginId)) { msg += "用户名已存在!! \n"; return false; } UserInfo.UserStateId = ConvertHelper.ToInt(UserSatateEnum.NormalState.GetHashCode()); return true; } public bool IsReusable { get { return false; } } bool IHttpHandler.IsReusable { get { throw new NotImplementedException(); } } }}