博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC学习十三:MVC3客户端自定义验证 -Jquery: addMinMax
阅读量:2385 次
发布时间:2019-05-10

本文共 2141 字,大约阅读时间需要 7 分钟。

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>

转载地址:http://pljab.baihongyu.com/

你可能感兴趣的文章
php pconnect 长连接原理
查看>>
php memcached使用中的坑
查看>>
php变量引用和计数_refcount_gc和is_ref_gc
查看>>
windows环境下php和Php扩展编译,扩展dll文件编译
查看>>
magento 验证码
查看>>
magento性能优化系列二:db篇
查看>>
Discuz!$_G变量的使用方法
查看>>
magento memcache缓存配置
查看>>
PHP json_encode中文乱码解决方法
查看>>
mysql服务启动、关闭
查看>>
php获取中文字符串的首字符拼音字母
查看>>
php curl通过代理获取数据
查看>>
6 个 Linux性能监控命令行工具
查看>>
mysql 编码字符集配置
查看>>
php查看opcode编码的扩展 opdumper
查看>>
php转换html格式为文本格式
查看>>
mysql-proxy主从服务架构下读写分离和负载均衡实现及原理
查看>>
Nginx location 和 rewrite retry
查看>>
基于nginx的FastCGI的缓存配置
查看>>
Nginx模块fastcgi_cache的几个注意点
查看>>