Dataannotations. validationattribute provides the following types
Stringlength, required, regularexpression, range, and datatype enable clientvalidation (HTML. enableclientvalidation), you can see that the output JS only contains the above types of validationtype. The custom validateionattribute does not have the corresponding validationtype in the output JS, which needs to be added by ourselves. For details, refer: 1. Define validationattribute. The native validationattribute provides a stringlength, but only the maximum length is limited. Here I add the minimum length and whether Trimpublic sealed class stringlengthrangeattribute: validationattribute {public int max {Get; private set;} public int min {Get; private set;} public bool trim {Get; private set ;} public stringlengthrangeattribute (INT min, int max): Base () {max = math. max (Min, max); min = math. min (Min, max);} public stringlengthrangeattribute (INT min, int Max, bool trim): Base () {max = math. max (Min, Ma X); min = math. min (Min, max); trim = trim;} public override bool isvalid (object Value) {If (value = NULL) return false; string v = trim? (String) value ). trim (): (string) value; If (v. length <min | v. length> MAX) {return false;} else return true;} public override string formaterrormessage (string name) {return errormessage;} 2. Define dataannotationsmodelvalidator public class stringlengthrangevalidator: dataannotationsmodelvalidator <stringlengthrangeattribute> {private int Max, min; private bool trim; private string errmsg; Public stringlengthrangevalidator (modelmetadata metadata, controllercontext CTX, stringlengthrangeattribute ATT): Base (metadata, CTX, ATT) {max = ATT. max; min = ATT. min; trim = ATT. trim; errmsg = ATT. errormessage;} public override ienumerable <modelclientvalidationrule> getclientvalidationrules () {modelclientvalidationrule rule = new modelclientvalidationrule {errormessage = errmsg, validation Type = "stringlengthrange"}; rule. validationparameters. add ("min", min); rule. validationparameters. add ("Max", max); rule. validationparameters. add ("trim", trim); return new [] {rule} ;}} at this time, the JS output in HTML will be more like: {"errormessage ": "password length must between 6 and 16. "," validationparameters ": {" min ": 6," Max ": 16," trim ": false}," validationtype ":" stringlengthrange "} 3, registration, modify global. asax. CS file, in Add dataannotationsmodelvalidatorprovider to application_start. registeradapter (typeof (stringlengthrangeattribute), typeof (stringlengthrangevalidator); 4. Write a JS file that contains the following content and reference this JS file if (string. trim! = NULL) string. prototype. trim = function () {return this. replace (/(^ \ s *) | (\ s * $)/g, "");} sys. MVC. validatorregistry. validators ["stringlengthrange"] = function (rule) {var min = rule. validationparameters ["min"]; var max = rule. validationparameters ["Max"]; var trim = rule. validationparameters ["trim"]; return function (value, context) {If (TRIM) value = value. trim (); var Len = value. length; If (LEN <Mi N | Len> MAX) return rule. errormessage; else return true ;}} OK, that's the process.