Asp.Mvc 2.0用户服务器验证实例讲解(4)(2)

/// <summary> /// 用户邮箱 /// </summary> [DisplayName("邮箱")] //[DataType(DataType.EmailAddress)] [RegularExpression(@"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$", ErrorMessage = "电子邮件格式错误")] public string Email { get; set; }


第一个参数邮箱验证的正则表达式,第二个参数是错误提示信息。
在ASPX页面显示错误信息用<%:Html.ValidationMessageFor(m=>m.Email)%>
以上是对用户注册信息的验证,当然,我们在提交信息的时候,要判断验证是否通过,我们使用ModelState.IsValid来判断验证是否通过,TRUE表示通过,FALSE表示未通过。
model代码:

/// <summary> /// 注册用户MODEL /// </summary> [PwdMatch("UserPwd", "ConfirPwd", ErrorMessage = "密码与确认不匹配")] public class RegisterModel { /// <summary> /// 用户名 /// </summary> [DisplayName("用户名")] [Required(ErrorMessage="用户名不能为空!")] public string UserName { get; set; } /// <summary> /// 密码 /// </summary> [DisplayName("密码")] [Required(ErrorMessage="密码不能为空")] [StringLength(10, ErrorMessage = "密码长度不能小于5位",MinimumLength=5)] public string UserPwd { get; set; } [DisplayName("确认密码")] [Required(ErrorMessage="确认密码不能为空!")] [StringLength(10, ErrorMessage = "确认密码长度不能小于5位",MinimumLength=5)] public string ConfirPwd { get; set; } /// <summary> /// 用户邮箱 /// </summary> [DisplayName("邮箱")] //[DataType(DataType.EmailAddress)] [RegularExpression(@"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$", ErrorMessage = "电子邮件格式错误")] public string Email { get; set; } } /// <summary> /// 此自定义类用于验证密码和确认密码必须一致 /// </summary> [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public class PwdMatch :ValidationAttribute { private object _typeid = new object(); public string PWD { get; set; }//密码 public string ConfirmPwd { get; set; }//确认密码 public PwdMatch(string pwd, string confirmPwd) : base() { PWD = pwd; ConfirmPwd = confirmPwd; } /// <summary> /// 返回错误的提示信息 /// </summary> /// <param></param> /// <returns></returns> public override string FormatErrorMessage(string name) { return ErrorMessage; } /// <summary> /// 重写TYPEID /// </summary> public override object TypeId { get { return _typeid; } } /// <summary> /// 判断是否想到 /// </summary> /// <param>value的值实际上是MODEL提交的MODEL类</param> /// <returns></returns> public override bool IsValid(object value) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value); object originalValue = properties.Find(PWD, true ).GetValue(value);//获取密码 object confirmValue = properties.Find(ConfirmPwd, true).GetValue(value);//获取确认密码的值 return Object.Equals(originalValue, confirmValue); } }

前台页面代码

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcLogin.Models.RegisterModel>" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>注册页面</title> <script type="text/javascript" src=""></script> <script type="text/javascript" src=""></script> <script type="text/javascript"> // $().ready(function () { // $("#form1").validate( // { // rules: // { // UserName: // { // required: true // }, // UserPwd: // { // required: true, // minlength: 6 // }, // ConfirPwd: // { // required: true, // minlength: 6, // equalTo: "#UserPwd" // }, // Email: // { // email: true // } // }, // messages: // { // UserName: // { // required: "<span>用户名不能为空! </span>" // }, // UserPwd: // { // required: "<span>密码不能为空!</span>", // minlength: jQuery.format("<span>密码长度不能小于{0}个字符!</span>") // }, // ConfirPwd: // { // required: "<span>确认密码不能为空!<span>", // minlength: jQuery.format("确认密码长度不能小于{0}个字符!"), // equalTo: "<span>两次输入密码不一致!</span>" // }, // Email: // { // email: "<span>邮箱输入格式不正确!</span>" // } // }, // onkeyup: false // }); // }); </script> </head> <body> <div> <br /> <p> <%if (ViewData["msg"] != null) {%> <%:ViewData["msg"]%> <%} %> </p> <br /> <p> <%:Html.ValidationSummary(true,"用户创建失败!") %> </p> <%Html.BeginForm("Register", "user", FormMethod.Post, new {,id="form1"}) ; %> <table> <tr> <td><%: Html.LabelFor(m => m.UserName) %></td> <td> <%: Html.TextBoxFor(m => m.UserName) %></td> <td><%:Html.ValidationMessageFor(m=>m.UserName) %></td> </tr> <tr> <td> <%: Html.LabelFor(m => m.UserPwd) %></td> <td> <%: Html.PasswordFor(m => m.UserPwd) %></td> <td><%:Html.ValidationMessageFor(m=>m.UserPwd) %></td> </tr> <tr> <td> <%: Html.LabelFor(m => m.ConfirPwd) %></td> <td> <%: Html.PasswordFor(m => m.ConfirPwd)%></td> <td><%:Html.ValidationMessageFor(m=>m.ConfirPwd) %></td> </tr> <tr> <td> <%: Html.LabelFor(m => m.Email) %></td> <td> <%: Html.TextBoxFor(m => m.Email) %></td> <td><%:Html.ValidationMessageFor(m=>m.Email) %></td> </tr> <tr> <td> <input type="submit" value="提交" /></td> <td></td> <td></td> </tr> </table> <%Html.EndForm(); %> </div> </body> </html>

controller代码

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwspw.html