Mvc model verification, mvc model

Source: Internet
Author: User
Tags valid email address

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.