1.MVC Verification
1.1. Verification method
Introducing namespaces
System . ComponentModel . dataannotations ;
Define validation rules:
The Model property adds a built-in validation feature that describes the top four of them:
[Required], [Stringlength], [Range], and [RegularExpression]
[stringlength(4,errormessage="The name you used is too long ~")] get set; }
Generated HTML code:
<input type= "text" value= "" name= "name" id= "Name" data-val-length-max= "4" data-val-length= "The name you used is too long ~" data-val= " True "class=" Text-box single-line ">
Client Authentication:
1. Introduction of JS Script support
<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>
2. Add a statement
@{html.enableclientvalidation (TRUE);} (MVC3 is turned on by default)
Server-Side validation:
Used in controller: Modelstate.isvalid to determine if validation is passed
* Define your own custom validation features, then apply them. You can define fully customized features by inheriting from the Validationattribute base class in the System.ComponentModel.DataAnnotations namespace.
public class Emailattribute:regularexpressionattribute
{
Public Emailattribute ()
: Base (@ "^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$ ")
{
}
}
1.2. Error message displayed:
Display after cell: @Html. Validationmessagefor (S=>s.name)
Concentrated in one place display: @Html. ValidationSummary ()