MVC3----Custom Client validation

Source: Internet
Author: User


1, Custom validation class:

Using system;using system.collections.generic;using system.linq;using system.web;using  system.componentmodel.dataannotations;using system.web.mvc;namespace schoolmanagedomw.models{     public class zdmaxlength : validationattribute,iclientvalidatable     {        private int MaxLength;         public zdmaxlength (int maxlength)              : base ("{0} character length is too long")          {            this. maxlength = maxlength;        }         /// <summary>        ///  Service-Side validation method          /// </summary>        /// <param name= " Value "></param>        /// <param name=" Validationcontext "></param>        /// <returns> </returns>        protected override validationresult  isvalid (Object value, validationcontext validationcontext)          {            if  (value  != null)             {                 if  (Convert.ToInt32 (value. ToString (). Length)  > maxlength)                 &nbsP {                     return new validationresult (Formaterrormessage (validationcontext.displayname));                 }             }             return ValidationResult.Success;        }          #region  IClientValidatable  Members          /// <summary>        ///  Implements the Iclientvalidatable interface. Client Authentication Methods         /// </summary>         /// <param name= "Metadata" ></param>         /// <param name= "Context" ></param>         /// <returns></returns>        public  Ienumerable<modelclientvalidationrule> getclientvalidationrules (modelmetadata metadata,  Controllercontext context)         {             var rule = new modelclientvalidationrule ();             rule. Errormessage = formaterrormessage ("Client"  + metadata. GetDisplayName ());             rule. validationtype =  "Zdmaxlength";             rule. Validationparameters.add ("MaxLength",  maxlength);             yield return rule;        }          #endregion     }}


2, use custom validation classes:

[Zdmaxlength (3)]public string Name {get; set;}


3, View Code:

①: You need to reference two scripts and a custom script:

<script type= "Text/javascript" src= "@Url. Content (" ~/scripts/jquery.validate.min.js ")" ></script>

<script type= "Text/javascript" src= "@Url. Content (" ~/scripts/jquery.validate.unobtrusive.min.js ")" ></ Script>

<script type= "Text/javascript" src= "@Url. Content (" ~/scripts/zd.js ")" ></script>


②:zd.js

Code hint///<reference path= "jquery-1.5.1-vsdoc.js"/>///<reference path= "Jquery.validate.js"/>///< Reference path= "Jquery.validate.unobtrusive.js"/>//zdmaxlength: Validator name, need to match Validationtype//value: Enter value//element:        INPUT element//zdmaxlength: Validation parameter $.validator.addmethod ("Zdmaxlength", function (value, element, Zdmaxlength) {if (value) {            if (value.tostring (). length > Zdmaxlength) {alert (value.tostring (). Length + "." + zdmaxlength);        return false; }}); $.validator.unobtrusive.adapters.addsingleval ("Zdmaxlength", "maxlength");


This article is from the "program Ape's Home--hunter" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1612073

MVC3----Custom Client validation

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.