For example: range, regularexpression, required, stringlength, and other verification attributes. These attributes greatly facilitate server-side verification. At the same time, we can also customize verification attributes to meet our special needs, the emergence of mvc3 further enhances the convenience of verification. Specifically, the ivalidatableobject and iclientvalidatable interfaces are added, by default, range, regularexpression, required, stringlength, and other verification attributes are supported for verification on the client and server.
Ivalidatableobject interface. I already have a lot of information on the Internet. Today I will mainly introduce the iclientvalidatable interface, which allows ASP.. Net MVC finds the supported client validators at runtime. This interface is used to support integration of different verification frameworks-from: Asp. net mvc3 overview.
The following describes how to use the iclientvalidatable interface to verify the client and server.
First, we define a verification attribute noisattribute. cs. The main function of this attribute is to verify that the user name cannot contain the specified string. The Code is as follows:
Public sealed class noisattribute: validationattribute {Public String inputstring {Get; set;} public noisattribute () {errormessage = "Verification Failed";} public override bool isvalid (object value) {If (value = NULL) return true; string inputstring = (string) value; If (inputstring. contains (inputstring) {return false;} return true ;}}
Next we will create an object class logon. CS
Public class logon {[displayname ("Age")] [range (6,100)] public int age {Get; set;} [displayname ("name")] [nois (inputstring = "Bu junsheng", errormessage = "User Name cannot contain Bu junsheng")] public string name {Get; Set ;}}
In logon. in CS, we apply the nois verification attribute to the name attribute, so that if the user enters "Bu junsheng" during logon, the verification will fail, however, this attribute can only be verified after the user submits the data to the server. (the age attribute applies the range verification attribute because Asp.net mvc3 supports client verification of range by default, so after the user enters the age, if the age does not meet the requirements, the verification will be performed on the client .), To enable noisattribute verification on the client, we need to implement the iclientvalidatable interface for noisattribute. CS, so we need to transform our noisattribute. CS code.
Public sealed class noisattribute: validationattribute, iclientvalidatable {Public String inputstring {Get; set;} public noisattribute () {errormessage = "Verification Failed ";} public ienumerable <modelclientvalidationrule> invoke (modelmetadata metadata, controllercontext context) {modelclientvalidationrule rule = new role {validationtype = "nois", errormessage = formaterrormessage (metadata. getdisplayname ()}; rule. validationparameters ["inputstring"] = inputstring; yield return rule;} public override bool isvalid (object Value) {If (value = NULL) return true; string inputstring = (string) value; If (inputstring. contains (inputstring) {return false;} return true ;}}
The iclientvalidatable interface is very simple. It only has one getclientvalidationrules () method. We only need to implement this method. This method includes two parameters. metadata indicates the metadata of the property to be verified, context indicates the context of the controller sending the request, and it returns a set of modelclientvalidationrule.
Let's take a look at this Code:
ModelClientValidationRule rule = new ModelClientValidationRule { ValidationType = "nois", ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()) };
This Code creates a modelclientvalidationrule object and assigns values to the two attributes. validationtype is the verification type (the client code needs to call this attribute), and errormessage is the error message.
Let's take a look at the following code:
rule.ValidationParameters["inputstring"] = InputString;
This Code adds an authentication attribute "inputstring" with the value of inputstring. It must be noted that validationparameters is an idictionary <string, Object> dictionary, the most important thing is that the key of validationparameters must be in lower case and cannot contain any upper-case letters. Otherwise, an error will be reported during running. Be sure to keep it in mind.
After iclientvalidatable. CS is complete, you need to write client code to verify both the client and the server.
By default, client authentication and hidden authentication are enabled for Asp.net mvc3. You can view this in Web. config.
<appSettings> <add key="ClientValidationEnabled" value="true"/> <add key="UnobtrusiveJavaScriptEnabled" value="true"/> </appSettings>
By default, Asp.net mvc3 uses the jquery validate plug-in for client verification, so you will see the code shown in the figure below in the View File.
<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>
Next we will write a custom verification rule for the validate plug-in and add the rule to unobtrusive. The Code is as follows:
<script type="text/javascript"> $.validator.addMethod("nois", function (value, element, param) { if (value == false) { return true; } if (value.indexOf(param) != -1) { return false; } else { return true; } }); $.validator.unobtrusive.adapters.addSingleVal("nois","inputstring");</script>
Note that the first parameter value of addmethod must be the same as that of iclientvalidatable. the value of validationtype in CS must be the same, both must be "nois", and $. validator. unobtrusive. adapters. addsingleval ("nois", "inputstring"); the second parameter value in this sentence must be the same as the key value of validationparameters and must be "inputstring".
In this way, the client and server can be verified at the same time. If you are interested, try again.