Model verification function, model Verification
Placeholder
{0} corresponds to the attribute {1} corresponding to minimum {2} corresponding to maximum [StringLength (15, MinimumLength = 6, errorMessage = "the password length must be between {2} and {1}")] create a resource file first. In order to achieve this verification effect, you can write it in the resource file and create a new class, this class mainly implements verification and other additional attributes. This class is a partial classification with the partial keyword added. Do not forget to add the annotation System. componentModel. dataAnnotations namespace
[MetadataType (typeof (Login_UserMetaData)] public partial class Login_User {// public class Login_UserMetaData is required for verification {[Display (Name = "username")] [Required (ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof (Resource)] public string UserName {get; set;} [Display (Name = "password")] [Required (ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof (Resource)] public string Password {get; set ;}}}
Then the Controller probably needs this code. Remember that the parameter type passed in by the action method is the class name. If the format is string username, ModelState. IsValid will be invalid.
[HttpPost] public ActionResult Login (Login_User loginUser) {if (! ModelState. isValid) {// return View ();} Login_User user = userManager if verification fails. login (loginUser. userName, loginUser. password); if (user = null) {// The user Password is incorrect! ViewBag. error = "incorrect user name or password! "; Return View ();} Session [" user "] = user; return RedirectToAction (" index "," home ");}
After doing so, you can implement server-side verification, but this is not enough. Here you still need to use client-side verification, which can greatly reduce the burden on the server. The code in view is as follows:
@ {ViewBag. Title = "background login";} @ model Letter. Data. Model. Login_User <script src = "~ /Scripts/jquery. validate. min. js "> </script> <script src = "~ /Scripts/jquery. validate. unobtrusive. min. js "> </script> @ using (Html. beginForm () {<div> <label for = "username"> User name: </label> @ Html. textBoxFor (u => u. userName) @ Html. validationMessage ("UserName") </div> <label for = "password"> password: </label> @ Html. passwordFor (u => u. password) @ Html. validationMessage ("Password") </div> <input type = "submit" value = "login"/> @ ViewBag. error </div>}
The following configuration should be available for web. config:
Attach a complete verification
Using System; using System. collections. generic; using System. data. entity; using System. linq; using System. web; using System. componentModel. dataAnnotations; namespace RegisterValidation. models {public partial class User {[Key] public int ID {get; set;} [Display (Name = "Name")] [Required (ErrorMessageResourceName = "Required ", errorMessageResourceType = typeof (Resource)] public string Name {get; set;} [Display (Name = "Birthday")] // [RegularExpression (@ "^ \ d {4} (\-| \/| \.) \ d {} \ 1 \ d {} $ ", ErrorMessageResourceName =" Regular ", ErrorMessageResourceType = typeof (Resource)] [Required (ErrorMessageResourceName =" Required ", errorMessageResourceType = typeof (Resource)] public DateTime? Birthday {get; set;} [Display (Name = "Age")] [Range (18, 60, ErrorMessageResourceName = "Range", ErrorMessageResourceType = typeof (Resource)] public int Age {get; set;} [Display (Name = "username")] [Required (ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof (Resource)] public string Username {get; set;} [Display (Name = "password")] [Required (ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof (Resource)] public string Password {get; set;} [Display (Name = "")] [Required (ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof (Resource)] [RegularExpression (@ "^ \ w + (-\ w +) | (\. \ w +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $ ", ErrorMessageResourceName =" Regular ", ErrorMessageResourceType = typeof (Resource)] public string Email {get; set ;}} public class UserDBContext: dbContext {public DbSet <User> Users {get; set ;}}}
Another part of the user class, including the Password Confirmation
Using System; using System. collections. generic; using System. linq; using System. web; using System. componentModel. dataAnnotations; namespace RegisterValidation. models {public partial class User {[Display (Name = "Confirm Password")] [Required (ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof (Resource)] [StringLength (10, minimumLength = 5, ErrorMessageResourceName = "StringLength", identifier = typeof (Resource)] [Compare ("Password", identifier = "Compare", ErrorMessageResourceType = typeof (Resource)] public string PasswordConfirm {get; set ;}}}
The resource file is as follows:
The effect is as follows: