MVC 数据验证收集代码
控制器 Home
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace MvcApplication3.Controllers 8 { 9 public class HomeController : Controller 10 { 11 public ActionResult Index() 12 { 13 ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。"; 14 15 return View(); 16 } 17 18 /// <summary> 19 /// 检查用户名是否有重复 20 /// </summary> 21 /// <param name="userName">用户在页面(视图)表单中输入的UserName</param> 22 /// <returns>Json</returns> 23 public ActionResult CheckUserName(string userName) 24 { 25 bool result = true; 26 27 if (userName == "admin") 28 result = false;//已存在 29 30 return Json(result, JsonRequestBehavior.AllowGet); 31 } 32 } 33 }
Model(UserInfo)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using System.Web; 8 9 using System.Web.Mvc; 10 using System.ComponentModel; 11 using System.ComponentModel.DataAnnotations; 12 namespace Model 13 { 14 /// <summary> 15 /// 这些例子就是验证的,注意此处没有客户端验证,完全是服务器端验证的,如果出错,视图会渲染再次显示,非常好用啊,很方便啊,要在以前你要写多少行代码来搞定这些繁琐无味的验证呢?微软太体恤程序员了,顶一个。 16 /// 在下面的例子当中,当数据发送到服务器验证不通过之后,则所有用户填写的内容在重新打开的页面后都清空了, 如果想不清空,即用户第一次填的内容还存在的话,可以return View(接受到的对象); 17 /// </summary> 18 public class UserInfo 19 { 20 public int Id { get; set; } //ID 21 /// <summary> 22 ///DisplayName:指定当前字段显示的名称 23 ///StringLength:指定允许的长度,括号里的第一个参数是最大长度,第二个参数是最小长度。最后一个参数是指:单字段值的长度不符合第一第二参数规定时候,提示错误的内容 24 ///Required:表示当前字段是必填选项,当提交的表单缺少该值就引发验证错误。括号里的AllowEmptyStrings是: 获取或设置一个值,该值指示是否允许空字符串。单值为false的时候表示当前字段的值不能为空 25 ///Remote:允许利用服务器端的回调函数执行客户端的验证逻辑。说白了就是支持AJAX验证。括号里的CheckUserName是Action方法。Home是控制器 ,它会调用Home控制中的CheckUserName方法来查询你所输入的用户名是否已经存在,如果存在,则提示"用户名已经存在" 26 /// </summary> 27 [DisplayName("用户名")] 28 [Required(AllowEmptyStrings = false, ErrorMessage = "用户名不能为空")] 29 [StringLength(10, MinimumLength = 2, ErrorMessage = "用户名长度必须在{2}和{1}位之间")] 30 [Remote("CheckUserName", "Home", ErrorMessage = "用户名已经存在")] 31 public string UserName { get; set; } //用户名 32 33 [DisplayName("用户密码")] 34 [DataType(DataType.Password)] //将密码已*号的形式来显示 35 [Required(AllowEmptyStrings = false, ErrorMessage = "密码不能为空")] 36 [StringLength(12, MinimumLength = 6, ErrorMessage = "密码长度必须在{2}和{1}位之间")] 37 public string UserPassword { get; set; } //用户密码 38 39 [DataType(DataType.Password)] 40 [System.ComponentModel.DataAnnotations.Compare("UserPassword", ErrorMessage = "密码不一致")] 41 [DisplayName("请再次确认密码")] 42 public string TUserPassword { get; set; } //再次输出密码 43 44 /// <summary> 45 /// RegularExpression:表示当前字段的值要符合一个正则表达式。如果不能匹配,则报一个验证错误。ErrorMessage的内容就是验证错误的具体信息。 46 /// </summary> 47 [Display(Name = "邮箱")] //也可以写成 [DisplayName("邮箱")] 48 [Required(AllowEmptyStrings = false, ErrorMessage = "邮箱不能为空")] 49 //[DataType(DataType.EmailAddress, ErrorMessage = "xxxxxxxx")] //其实也可以用以下的正则表达式来验证邮箱 50 [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9]+\.[A-Za-z]{2,4}", ErrorMessage = "{0}的格式不正确")] 51 public string Email { get; set; } //邮箱 52 53 [DisplayName("请再次确认邮箱")] 54 [System.ComponentModel.DataAnnotations.Compare("Email", ErrorMessage = "两次输入的邮箱不一致")] 55 //[DataType(DataType.EmailAddress, ErrorMessage = "请输入有效的邮箱地址")] 56 public string TEmail { get; set; } //再次输入邮箱 57 58 [DisplayName("身份证")] 59 [RegularExpression(@"\d{17}[\d|x]|\d{15}", ErrorMessage = "身份证号码格式错误")] 60 public string IdentityNo { get; set; } //身份证号 61 62 /// <summary> 63 /// Range:用来指定数值类型值的最小值和最大值。括号里的第一个数的“最小值”第二个参数是“最大值” 64 /// </summary> 65 [DisplayName("年龄")] 66 [Required(AllowEmptyStrings = false, ErrorMessage = "年龄不能为空")] 67 [Range(10, 120, ErrorMessage = "您输入的年龄不符合规范,年龄应该在{1}-{2}之间")] 68 public int Age { get; set; } //年龄 69 70 /// <summary> 71 /// DisplayFormat:用来处理属性的各种格式化选项。当属性包含空值时,可以提供可选的显示文本,也可以为包含标记的属性关闭HTML编码,还可以为运行时指定一个应用于属性值的格式化字符串。 72 /// </summary> 73 [DisplayName("金额")] 74 [DataType(DataType.Currency)] //Currency:表示货币值 75 [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")] 76 [Required(ErrorMessage = "金额不能为空")] 77 [Range(typeof(decimal), "20.0", "30.0", ErrorMessage = "金额在{1}和{2}之间")] 78 public decimal Money { get; set; } //金额 79 80 [Display(Name = "手机号码")] 81 [DataType(DataType.PhoneNumber)] 82 [RegularExpression(@"((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)", ErrorMessage = "格式不正确")] 83 public int Phome { get; set; } 84 85 86 [Display(Name = "生日")] 87 //[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString="yyyy/MM/dd")] 88 [Required] //当前字段的值不能为空 89 public DateTime Birthday { get; set; } //生日 90 91 [Display(Name = "备注")] 92 [DataType(DataType.MultilineText)] //当前字段是个多行文本 93 public string Remarks { get; set; } //备注 94 95 96 } 97 }
View (Index)
1 @model Model.UserInfo 2 @{ 3 ViewBag.Title = "主页"; 4 } 5 <script src="~/Scripts/jquery-1.7.1.min.js"></script> 6 <script src="~/Scripts/jquery.validate.min.js"></script> 7 <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> 8 @using (Html.BeginForm()) 9 { 10 @Html.ValidationSummary(true) 11 12 <fieldset> 13 <legend>UserInfo</legend> 14 15 <div class="editor-label"> 16 @Html.LabelFor(model => model.UserName) 17 </div> 18 <div class="editor-field"> 19 @Html.EditorFor(model => model.UserName) 20 @Html.ValidationMessageFor(model => model.UserName) 21 </div> 22 23 <div class="editor-label"> 24 @Html.LabelFor(model => model.UserPassword) 25 </div> 26 <div class="editor-field"> 27 @Html.EditorFor(model => model.UserPassword) 28 @Html.ValidationMessageFor(model => model.UserPassword) 29 </div> 30 31 <div class="editor-label"> 32 @Html.LabelFor(model => model.TUserPassword) 33 </div> 34 <div class="editor-field"> 35 @Html.EditorFor(model => model.TUserPassword) 36 @Html.ValidationMessageFor(model => model.TUserPassword) 37 </div> 38 39 <div class="editor-label"> 40 @Html.LabelFor(model => model.Email) 41 </div> 42 <div class="editor-field"> 43 @Html.EditorFor(model => model.Email) 44 @Html.ValidationMessageFor(model => model.Email) 45 </div> 46 47 <div class="editor-label"> 48 @Html.LabelFor(model => model.TEmail) 49 </div> 50 <div class="editor-field"> 51 @Html.EditorFor(model => model.TEmail) 52 @Html.ValidationMessageFor(model => model.TEmail) 53 </div> 54 55 <div class="editor-label"> 56 @Html.LabelFor(model => model.IdentityNo) 57 </div> 58 <div class="editor-field"> 59 @Html.EditorFor(model => model.IdentityNo) 60 @Html.ValidationMessageFor(model => model.IdentityNo) 61 </div> 62 63 <div class="editor-label"> 64 @Html.LabelFor(model => model.Age) 65 </div> 66 <div class="editor-field"> 67 @Html.EditorFor(model => model.Age) 68 @Html.ValidationMessageFor(model => model.Age) 69 </div> 70 71 <div class="editor-label"> 72 @Html.LabelFor(model => model.Money) 73 </div> 74 <div class="editor-field"> 75 @Html.EditorFor(model => model.Money) 76 @Html.ValidationMessageFor(model => model.Money) 77 </div> 78 79 <div class="editor-label"> 80 @Html.LabelFor(model => model.Phome) 81 </div> 82 <div class="editor-field"> 83 @Html.EditorFor(model => model.Phome) 84 @Html.ValidationMessageFor(model => model.Phome) 85 </div> 86 87 <div class="editor-label"> 88 @Html.LabelFor(model => model.Birthday) 89 </div> 90 <div class="editor-field"> 91 @Html.EditorFor(model => model.Birthday) 92 @Html.ValidationMessageFor(model => model.Birthday) 93 </div> 94 95 <div class="editor-label"> 96 @Html.LabelFor(model => model.Remarks) 97 </div> 98 <div class="editor-field"> 99 @Html.EditorFor(model => model.Remarks) 100 @Html.ValidationMessageFor(model => model.Remarks) 101 </div> 102 103 <p> 104 <input type="submit" value="Create" /> 105 </p> 106 </fieldset> 107 } 108 109 <div> 110 @Html.ActionLink("Back to List", "Index") 111 </div>
最终效果