MVC3----自訂用戶端驗證

來源:互聯網
上載者:User

標籤:用戶端   服務端   private   public   


1,自訂驗證類:

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}字元長度過長")        {            this.MaxLength = maxlength;        }        /// <summary>        /// 服務端驗證方法        /// </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)                {                    return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));                }            }            return ValidationResult.Success;        }        #region IClientValidatable 成員        /// <summary>        /// 實現IClientValidatable介面。用戶端驗證方法        /// </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("用戶端" + metadata.GetDisplayName());            rule.ValidationType = "zdmaxlength";            rule.ValidationParameters.Add("maxlength", MaxLength);            yield return rule;        }        #endregion    }}


2,使用自訂驗證類:

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


3,視圖代碼:

①:需要引用兩個指令碼和一個自訂指令碼:

<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

////代碼提示///<reference path="jquery-1.5.1-vsdoc.js" />///<reference path="jquery.validate.js" />///<reference path="jquery.validate.unobtrusive.js" />//zdmaxlength:驗證器名稱,需要匹配ValidationType//value:輸入值//element:輸入元素//zdmaxlength:驗證參數$.validator.addMethod("zdmaxlength", function (value, element, zdmaxlength) {    if (value) {        if (value.toString().length > zdmaxlength) {            alert(value.toString().length + "." + zdmaxlength);            return false;        }    }    return true;});$.validator.unobtrusive.adapters.addSingleVal("zdmaxlength", "maxlength");


本文出自 “程式猿的家--Hunter” 部落格,請務必保留此出處http://962410314.blog.51cto.com/7563109/1612073

MVC3----自訂用戶端驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.