Mvc model verification, mvc model
1. Enable client verification (jquery verification ):
1. web. config:
<add key="ClientValidationEnabled" value="true"/><add key="UnobtrusiveJavaScriptEnabled" value="true"/>
2. The verification page references jquery:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
3. Display messages. true indicates that all messages are displayed.
Verification message displayed at ValidationMessageFor
@Html.ValidationSummary(true)
@Html.ValidationMessageFor(m => m.UserName)
4. Add the field to be verified above the model class[Required],In addition to Required, we can also add other useful verification tags to the Model. The following is a complete list:
ModelVerification tags that can be added to the class:
1. required field [Required] public string FirstName {get; set;} 2. the maximum length of a field is n bits: [StringLength (160)] public string FirstName {get; set;} requires at least n bits: [StringLength (160, MinimumLength = 3)] public string FirstName {get; set;} 3. regular Expression validation [RegularExpression (@ "[A-Za-z0-9. _ % +-] + @ [A-Za-z0-9. -] + \. [A-Za-z] {2, 4} ")] public string Email {get; set;} 4. range [Range (0.00)] public int Age {get; set;}: [Range (typeof (decimal), "49.99", "")] public decimal Price {get; set;} 5. the server participates in the verification [Remote ("CheckUserName", "Account")] public string UserName {get; set;} And then specifies a CheckUserName method in AccountController: public JsonResult CheckUserName (string username) {var result = Membership. findUsersByName (username ). count = 0; return Json (result, JsonRequestBehavior. allowGet);} 6. compare [RegularExpression (@ "[A-Za-z0-9. _ % +-] + @ [A-Za-z0-9. -] + \. [A-Za-z] {2, 4} ")] public string Email {get; set;} [Compare (" Email ")] public string EmailConfirm {get; set ;} 7. custom error message RegEx: [RegularExpression (@ "[A-Za-z0-9. _ % +-] + @ [A-Za-z0-9. -] + \. [A-Za-z] {2, 4} ", ErrorMessage =" Email doesn' t look like a valid email address. ")] public string Email {get; set;} common text: [Required (ErrorMessage =" Your last name is required ")] [StringLength (160, errorMessage = "Your last name is too long")] public string LastName {get; set;} placeholder: [Required (ErrorMessage = "Your {0} is required. ")] [StringLength (160, ErrorMessage =" {0} is too long. ")] public string LastName {get; set ;}View Code
2. Background verification (client-only verification is definitely not secure)
[HttpPost] public ActionResult Login (UserModel m) {if (string. IsNullOrEmpty (m. UserName) {ModelState. AddModelError ("UserName", "UserName field is required. "); // The verification prompt is displayed on the page.} if (string. IsNullOrEmpty (m. Password) {ModelState. AddModelError (" Password "," the Password field is required. "); // The verification prompt is displayed on the page.} if (ModelState. IsValid) // you can check whether the verification passes {} return View ();}View Code