ASP.NET MVC——序列之【身份证验证】

1.创建新类继承【ValidationAttribute】类

2.首先引用程序集【System.ComponentModel.DataAnnotations】

3.导入命名空间 using System.ComponentModel.DataAnnotations;

4.重新【IsValid】方法

5.

 1  protected override ValidationResult IsValid(object value, ValidationContext validationContext)
 2         {
 3 
 4             if (str.Length == 0)
 5             {
 6                 return new ValidationResult(validationContext.DisplayName + "不能为空");//数字验证
 7             }
 8             string str = value.ToString();
 9            
10              if (str.Length == 18)
11             {
12                 long n = 0;
13                 if (long.TryParse(str.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(str.Replace('x', '0').Replace('X', '0'), out n) == false)
14                 {
15                     return new ValidationResult(validationContext.DisplayName + "包含非法字符");//数字验证
16                 }
17                 string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
18                 if (address.IndexOf(str.Remove(2)) == -1)
19                 {
20                     return new ValidationResult(validationContext.DisplayName + "地区不合法!");//省份验证
21                 }
22                 string birth = str.Substring(6, 8).Insert(6, "-").Insert(4, "-");
23                 DateTime time = new DateTime();
24                 if (DateTime.TryParse(birth, out time) == false)
25                 {
26                     return new ValidationResult(validationContext.DisplayName + "出生日期不通过!");//生日验证
27                 }
28                 string[] arrVarifyCode = ("1,0,x,9,8,7,ralph lauren home store,6,5,4,3,2").Split(',');
29                 string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
30                 char[] Ai = str.Remove(17).ToCharArray();
31                 int sum = 0;
32                 for (int i = 0; i < 17; i++)
33                 {
34                     sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
35                 }
36                 int y = -1;
37                 Math.DivRem(sum, 11, out y);
38                 if (arrVarifyCode[y] != str.Substring(17, 1).ToLower())
39                 {
40                     return new ValidationResult(validationContext.DisplayName + "校验码不通过!");//校验码验证
41                 }
42                 return ValidationResult.Success;//契合GB11643-1999标准
43 
44             }
45             else if (str.Length == 15)
46             {
47                 long n = 0;
48                 if (long.TryParse(str, out n) == false || n < Math.Pow(10, 14))
49                 {
50                     return new ValidationResult(validationContext.DisplayName + "包含非法字符!");//数字验证
51                 }
52                 string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
53                 if (address.IndexOf(str.Remove(2)) == -1)
54                 {
55                     return new ValidationResult(validationContext.DisplayName + "地区不合法!");//省份验证
56                 }
57                 string birth = str.Substring(6, 6).Insert(4, "-").Insert(2, "-");
58                 DateTime time = new DateTime();
59                 if (DateTime.TryParse(birth, out time) == false)
60                 {
61                     return new ValidationResult(validationContext.DisplayName + "出生日期不通过!");//生日验证
62                 }
63                 return ValidationResult.Success;//契合GB11643-1999标准
64             }
65             else
66             {
67                 return new ValidationResult(validationContext.DisplayName + "位数不符合!");//生日验证
68             
69             }
70 
71 
72 
73         }
View Code

 

posted @ 2015-05-17 14:33  寒冷记忆  阅读(923)  评论(0编辑  收藏  举报