1.幫助類


1 public static class MVCHelper
2 {
3 public static string GetValiMsg(ModelStateDictionary modelState)
4 {
5 StringBuilder sb = new StringBuilder();
6 foreach (var propName in modelState.Keys)
7 {
8 if (modelState[propName].Errors.Count<=0)
9 {
10 continue;
11 }
12 sb.Append("屬性驗(yàn)證錯誤: ").Append(propName).Append(": ");
13 foreach (ModelError item in modelState[propName].Errors)
14 {
15 sb.Append(item.ErrorMessage);
16 }
17 }
18 return sb.ToString();
19 }
20 }
21
22 /// <summary>
23 /// 正則設(shè)置特性驗(yàn)證
24 /// </summary>
25 public class QQValiAttribute: RegularExpressionAttribute
26 {
27 public QQValiAttribute():base(@"^\d{5,12}$")
28 {
29 this.ErrorMessage = "只能是5-12位";
30 }
31 }
32
33 /// <summary>
34 /// 繼承ValidationAttribute 重寫IsValid驗(yàn)證
35 /// </summary>
36 public class CNPhone : ValidationAttribute
37 {
38 public override bool IsValid(object value)
39 {
40 if (value is string)
41 {
42 string v = (string)value;
43 if (v.Length==11)
44 {
45 if (v.StartsWith("13")|| v.StartsWith("18")|| v.StartsWith("16"))
46 {
47 return true;
48 }
49 else
50 {
51 this.ErrorMessage = "不是中國手機(jī)號";
52 return false;
53 }
54 }
55 else
56 {
57 this.ErrorMessage = "位數(shù)不正確";
58 return false;
59 }
60 }
61 else
62 {
63 this.ErrorMessage = "不是合法的手機(jī)號";
64 return false;
65 }
66
67 }
682.控制器


1 public class TestController : Controller
2 {
3 // GET: Test
4 public ActionResult Index()
5 {
6 return View();
7 }
8
9 public ActionResult demo01()
10 {
11 ViewBag.Price = 10;
12 return View();
13 }
14 public ActionResult demo02(Person model)
15 {
16 if (ModelState.IsValid)
17 {
18 return Content("Age="+model.Age+" id="+model.Id);
19 }
20 else
21 {
22 string msg = MVCHelper.GetValiMsg(ModelState);
23 return Content(msg);
24 }
25
26 }
273.model


1 public class Person
2 {
3 [Range(1000,4442)]
4 public int Age { get; set; }
5 [Required]
6 [QQValiAttribute]
7 public int Id { get; set; }
8
9 [CNPhone]
10 public string Phone { get; set; }
114.頁面


1 @{
2 Layout = null;
3 }
4
5 <!DOCTYPE html>
6
7 <html>
8 <head>
9 <meta name="viewport" content="width=device-width" />
10 <title>demo02</title>
11 </head>
12 <body>
13 <div>
14 </div>
15 </body>
16 </html


1 public static class MVCHelper
2 {
3 public static string GetValiMsg(ModelStateDictionary modelState)
4 {
5 StringBuilder sb = new StringBuilder();
6 foreach (var propName in modelState.Keys)
7 {
8 if (modelState[propName].Errors.Count<=0)
9 {
10 continue;
11 }
12 sb.Append("屬性驗(yàn)證錯誤: ").Append(propName).Append(": ");
13 foreach (ModelError item in modelState[propName].Errors)
14 {
15 sb.Append(item.ErrorMessage);
16 }
17 }
18 return sb.ToString();
19 }
20 }
21
22 /// <summary>
23 /// 正則設(shè)置特性驗(yàn)證
24 /// </summary>
25 public class QQValiAttribute: RegularExpressionAttribute
26 {
27 public QQValiAttribute():base(@"^\d{5,12}$")
28 {
29 this.ErrorMessage = "只能是5-12位";
30 }
31 }
32
33 /// <summary>
34 /// 繼承ValidationAttribute 重寫IsValid驗(yàn)證
35 /// </summary>
36 public class CNPhone : ValidationAttribute
37 {
38 public override bool IsValid(object value)
39 {
40 if (value is string)
41 {
42 string v = (string)value;
43 if (v.Length==11)
44 {
45 if (v.StartsWith("13")|| v.StartsWith("18")|| v.StartsWith("16"))
46 {
47 return true;
48 }
49 else
50 {
51 this.ErrorMessage = "不是中國手機(jī)號";
52 return false;
53 }
54 }
55 else
56 {
57 this.ErrorMessage = "位數(shù)不正確";
58 return false;
59 }
60 }
61 else
62 {
63 this.ErrorMessage = "不是合法的手機(jī)號";
64 return false;
65 }
66
67 }
68


1 public class TestController : Controller
2 {
3 // GET: Test
4 public ActionResult Index()
5 {
6 return View();
7 }
8
9 public ActionResult demo01()
10 {
11 ViewBag.Price = 10;
12 return View();
13 }
14 public ActionResult demo02(Person model)
15 {
16 if (ModelState.IsValid)
17 {
18 return Content("Age="+model.Age+" id="+model.Id);
19 }
20 else
21 {
22 string msg = MVCHelper.GetValiMsg(ModelState);
23 return Content(msg);
24 }
25
26 }
27


1 public class Person
2 {
3 [Range(1000,4442)]
4 public int Age { get; set; }
5 [Required]
6 [QQValiAttribute]
7 public int Id { get; set; }
8
9 [CNPhone]
10 public string Phone { get; set; }
11


1 @{
2 Layout = null;
3 }
4
5 <!DOCTYPE html>
6
7 <html>
8 <head>
9 <meta name="viewport" content="width=device-width" />
10 <title>demo02</title>
11 </head>
12 <body>
13 <div>
14 </div>
15 </body>
16 </html