ASP.NET MVC——序列之【身份證驗證】

1.建立新類繼承【ValidationAttribute】類ide

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

3.導入命名空間 using System.ComponentModel.DataAnnotations;code

4.從新【IsValid】方法blog

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
相關文章
相關標籤/搜索