MVC provides very handy data validation, so you can generate relevant validation scripts in the foreground by simply adding the relevant regular in the model. You need to refer to two JS files:
Jquery.validate.min.js
Jquery.validate.unobtrusive.min.js
Model Data Validation Rollup:
[Display (Name ="Transfer Amount")] [Required (Allowemptystrings=false, errormessage ="Please enter the amount of the transfer")] [Range ( -,20000, errormessage ="The amount entered can only be between $100 to $20,000")] [RegularExpression (@"^[1-9]\d*$", errormessage ="The amount transferred must be an integer")] Public decimalAmount {Get;Set; } /// <summary> ///Transaction Password/// </summary>[Display (Name ="Transaction Password")] [Stringlength ( -, Minimumlength =6, errormessage ="{ 0} has a length of {2} to {1} characters")] [Required (Allowemptystrings=false, errormessage ="Please enter the transaction password")] [DataType (Datatype.password)] Public stringTradingpassword {Get;Set; } [Stringlength (Ten, Minimumlength =4, errormessage ="{0} must have a length greater than {2} characters and less than {1} characters")] [Display (Name="Password")] Public Virtual stringLogpassword {Get;Set; } [Stringlength (Ten, errormessage ="{0} cannot have a length greater than {1} characters")] [Display (Name="name")] Public Virtual stringRealname {Get;Set; } [Display (Name="Date of birth")] //[Range (typeof (DateTime), "2011-12-31", "1950-1-1", errormessage = "{0} is scoped to {1} to {2}")] Public VirtualDateTime Birthday {Get;Set; } [RegularExpression (@"\d{17}[\d| X]|\D{15}", errormessage ="the {0} is not in the correct format")] [Display (Name="ID Number")] Public Virtual stringIdentityno {Get;Set; } [RegularExpression (@"[A-za-z0-9._%+-][email protected][a-za-z0-9.-]+\. [A-za-z] {2,4}", errormessage ="the {0} is not in the correct format")] [Display (Name="Email")] Public Virtual stringEmail {Get;Set; } [Required (ErrorMessage="The registrant mailbox cannot be empty.")] [RegularExpression (@"^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$", errormessage ="The mailbox is not correct")] Public stringEmail {Get;Set; }
Original address: http://www.cnblogs.com/jys509/p/4111034.html
. NET MVC Model data validation