当前位置:编程学习 > asp >>

ASP.NET MVC 3 直到我膝盖中了一箭【5】模型验证

1.非空和数据类型

 1         [Required]
 2         [Display(Name = "用户名")]
 3         public string UserName { get; set; }
 4
 5         [Required]
 6         [DataType(DataType.Password)]
 7         [Display(Name = "密码")]
 8         public string Password { get; set; }
 9
10         [Required]
11         [DataType(DataType.EmailAddress)]
12         [Display(Name = "电子邮件地址")]
13         public string Email { get; set; }

2.非空和字符长度 www.zzzyk.com

1         [Required(ErrorMessage="用户名不能为空!")]
2         [DisplayName("用户名")]
3         public string UserName { get; set; }
4
5         [DisplayName("密码")]
6         [StringLength(6,ErrorMessage="密码长度不能超过6个字符!")]
7         public string Password { get; set; }

3.值域
1         [DisplayName("年龄")]
2         [Range(1,int.MaxValue,ErrorMessage="年龄不能小于1!")]       
3         public int Age { get; set; }
4.比较

1         [Required]
2         [DataType(DataType.Password)]
3         [DisplayName("密码")]
4         public string Password { get; set; }
5
6         [DataType(DataType.Password)]
7         [DisplayName("确认密码")]
8         [Compare("Password", ErrorMessage = "密码和确认密码不匹配!")]
9         public string ConfirmPassword { get; set; }

5.正则表达式

1         [DisplayName("联系电话")]
2         [RegularExpression(@"^((0\d{2,5}-)|\(0\d{2,5}\))?\d{7,8}(-\d{3,4})?$",ErrorMessage = "电话格式不正确!\n 有效格式为:\n①本区7或8位号码[-3或4位分机号码,可选]\n②(3~5位区号)7或8位号码[-3或4位分机号码,可选]\n③3~5位区号-7或8位号码[-3或4位分机号码,可选]\n示例:023-12345678;(023)1234567-1234")]
3         public string Phone { get; set; }
4         [DisplayName("电子邮件")]
5         [RegularExpression(@"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$", ErrorMessage = "请输入正确的Email格式!\n示例:abc@123.com")]
6         public string Email { get; set; }
7         [DisplayName("网址")]
8         [RegularExpression(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", ErrorMessage = "请输入合法的网址!\n示例:https://abc.com;http://www.abc.cn")]
9         public string Httpaddress { get; set; }

6.自定义
1         [Required]
2         [ValidatePasswordLength]
3         [DataType(DataType.Password)]
4         [DisplayName("密码")]
5         public string Password { get; set; }

 1     [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
 2     public sealed class ValidatePasswordLengthAttribute : ValidationAttribute, IClientValidatable
 3     {
 4         private const string _defaultErrorMessage = "'{0}' 必须至少包含 {1} 个字符。";
 5         private readonly int _minCharacters = Membership.Provider.MinRequiredPasswordLength;
 6
 7         public ValidatePasswordLengthAttribute()
 8             : base(_defaultErrorMessage)
 9         {
10         }
11
12         public override string FormatErrorMessage(string name)
13         {
14             return String.Format(CultureInfo.CurrentCulture, ErrorMessageString,
15                 name, _minCharacters);
16         }
17
18         public override bool IsValid(object value)
19         {
20             string valueAsString = value as string;
21             return (valueAsString != null && valueAsString.Length >= _minCharacters);
22         }
23
24         public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
25         {
26             return new[]{
27                 new ModelClientValidationStringLengthRule(FormatErrorMessage(metadata.GetDisplayName()), _minCharacters, int.MaxValue)
28             };
29         }
30     }

7.其他

作者 依信依誉

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,