Added support for custom client validation Javascript in MVC

Source: Internet
Author: User

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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.