Validating the information entered by the user is an important but tedious task for web developers, and many developers will ignore it. The ASP.net mvc3 framework uses a method called "Data Annotations" (dataannotations) for data validation. This allows the program to perform double validation on both the client and server side (the asp.net data validation control is also doubly validated). The benefit of dual authentication is that client-side validation is directly associated with the user, reducing the pressure on the server and improving the user experience without submitting the form, while server-end validation ensures the validity and integrity of the data, because sometimes the client turns off the scripting feature. Note: To implement client-side validation, you need to refer to jquery and the jquery validation file:
<script src= "@Url. Content (" ~/scripts/jquery-1.5.1.min.js ")" Type= "Text/javascript" ></script>
<script src= "@Url. Content (" ~/scripts/jquery.validate.min.js ")" Type= "Text/javascript" ></script>
<script src= "@Url. Conten (" ~/scripts/jquery.validate.unobtrusive.min.js ")" Type= "Text/javascript" ></" Script>
To implement server-side validation, you need to perform modelstate.isvalid first, basic validation 1, Require (non-null authentication) Model after submitting the form:
[Required]
[Display (name = "user name")]
public string UserName {get; set;}
Validation results:
2, Stringlength (string length verification) can verify the model properties of the maximum length and minimum length, respectively corresponding to MaximumLength and Minimumlength, where minimumlength is optional. Model:
[Required]
[Stringlength (minimumlength = 6)]
[DataType (Datatype.password)]
[Display (Name = "password")]
public string Password {get; set;}
Validation results
3, RegularExpression (regular expression) verifies the attributes that conform to the regular expression. Model:
[Required]
[DataType (datatype.emailaddress)]
[RegularExpression (@] [a-za-z0-9._%+-]+@[a-za-z0-9._]+\.[ a-za-z]{2,4} ")]
[Display (Name =" e-mail Address ")] public
string Email {get; set;}
Validation results:
4. Range (numerical range validation) is used to specify the maximum and minimum values for the numeric value, the first parameter is the minimum value, and the second parameter is the maximum (including themselves). You can have a user int type and a double type. Model:
[Required]
[Range (a)]
[Display (name= "age")]
public int Age {get; set;}
Validation results:
Second, additional validation additional validation is the additional two validation features added to the SYSTEM.WEB.MVC, so you must add a using SYSTEM.WEB.MVC 1, Remote Authentication Although ASP.net MVC3 provides a lot of Del in the direct validation of the data characteristics, but a lot of logic complex validation is not in the model to verify, so the MVC3 framework provides this remote validation feature, he allows developers in the controller in C # code to verify the validity of the data. A classic application verifies that the user name is duplicated. This data validation cannot be validated on the client side unless all user names are sent to the client (which is obviously not possible). So the remote feature is only server-side validation. But it is validated asynchronously, so there is a better user experience. Model:
[Required]
[Display (name = "user name")]
[Remote ("Checkusername", "Account")]
public string UserName {get; set;}
Controller:
Public Jsonresult Checkusername (string userName)
{
var = userName = = "admin";
return Json (result, jsonrequestbehavior.allowget);
}
Validation results: NOTE: If you want to post the submission, you need to add the httpmethod= "POST":
[Required (Errormessageresourcetype=typeof (errormessage), errormessageresourcename= "Userrequire")]
[Display (name = "user name")]
[Remote ("Checkusername", "Account", httpmethod= "POST")]
public string UserName {get; set;}
and controller can directly accept the POST request, of course, you add [HttpPost] also can:
[HttpPost] public
jsonresult checkusername (string userName)
{
var = userName!= "admin";
return Json (result, jsonrequestbehavior.allowget);
}
2, Compare (same authentication) The Compare feature is used to verify that the two data entered is exactly the same. The most typical example is the same password that was entered twice at registration. Model:
[Required]
[Stringlength errormessage = ' {0} must contain at least {2} characters. ", Minimumlength = 6)]
[DataType (Datatype.password)]
[Display (Name = ' password ')]] public
string Password {get; Set [
DataType (Datatype.password)]
[Display (Name = "Confirm password")]
[Compare ("Password", errormessage = " The password and Confirm password do not match. )] public
string ConfirmPassword {get; Set }
Validation results:
Above is the ASP.net mvc3 framework of several commonly used data annotations, in addition to the latter two unique features of MVC, other features are in the System.ComponentModel.DataAnnotations, the appropriate use will greatly improve the development efficiency. Specific See also: http://msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.aspx