欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-30 21:14:21
1.非空和数据类型  1         [required]  2&nbs...

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.非空和字符长度

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示例:")]
6         public string email { get; set; }
7         [displayname("网址")]
8         [regularexpression(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", errormessage = "请输入合法的网址!\n示例:")]
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.其他

作者 依信依誉