MVC学习十三:MVC3客户端自定义验证 -Jquery: addMinMax

1.  自定义PriceAttribute

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace MvcApp.Web.Classes
{
    public sealed class PriceAttribute : ValidationAttribute, IClientValidatable
    {
        public double MinPrice { get; set; }
        public double MaxPrice { get; set; }

        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return true;
            }
            var price = (double) value;
            if (price < MinPrice || price > MaxPrice)
            {
                return false;
            }
            double cents = price - Math.Floor(price);
            if (cents < 0.99 || cents >= 0.995)
            {
                return false;
            }

            return true;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = this.ErrorMessage,
                ValidationType = "price"
            };
            rule.ValidationParameters.Add("min", this.MinPrice);
            rule.ValidationParameters.Add("max", this.MaxPrice);

            yield return rule;
        }

    }
}

2. 应用到实体

        [Price(MinPrice = 3.99, MaxPrice = 9.99, ErrorMessage = "Price must end in .99 and be larger than 3.99 and less than 9.99")]
        public double Price { get; set; }

 

3. 客户端定义注册

<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>
<script type="text/javascript">
    $(function () {
        $.validator.addMethod("priceMethod", function (value, element, params) {
            if (this.optional(element)) {
                return true;
            }

            //alert(value);
            //alert(params);
            //alert(params[0]);

            var minValue = parseFloat(params[0]);
            var maxValue = parseFloat(params[1]);
           
            if (value > minValue && value < maxValue) {
                var cents = value - Math.floor(value);
                if (cents >= 0.99 && cents < 0.995) {
                    return true;
                }
            }

            return false;
        });
    });

    //$.validator.unobtrusive.adapters.addSingleVal("price", "min");
第一个参数是适配器的名称,必须与服务器端设置规则的ValidationProperty的值相同。第二个参数是用来检索元数据的参数名称。请注意,不需要在参数名中使用data-前缀;这里的参数需要与你在服务器端设置的ValidationParameters集合中的参数名相匹配。

    $.validator.unobtrusive.adapters.addMinMax("price", "min", "max", "priceMethod");
   
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值