【问题标题】:ASP.net MVC3 Client side DateComparison custom validation attribute not working correctly in current stable version of Chrome?ASP.net MVC3 客户端 DateComparison 自定义验证属性在当前稳定版本的 Chrome 中无法正常工作?
【发布时间】:2011-11-01 09:24:17
【问题描述】:

我的项目中有一个情景,我必须比较日期才能确定开始日期是否大于结束日期。到目前为止,MVC3 没有提供自己的属性来比较日期。所以为此我创建了自己的自定义属性来比较日期 下面给出了

命名空间 WebUtil.Validators { 使用系统; 使用 System.Collections.Generic; 使用 System.ComponentModel.DataAnnotations; 使用 System.Web.Mvc; 使用 WebUtil.Resources;

public class CompareDateAttribute : ValidationAttribute, IClientValidatable
{

    public CompareDateAttribute(string otherPropertyName)
        : base(WebUtilResources.Validation_Invalid_Dates)
    {
        this.OtherControlName = otherPropertyName;
    }

    public string OtherControlName { get; set; }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(ErrorMessageString, name, this.OtherControlName);
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
        ModelMetadata metadata, ControllerContext context)
    {
        var greaterRule = new ModelClientValidationRule();
        greaterRule.ErrorMessage = this.FormatErrorMessage(metadata.GetDisplayName());
        greaterRule.ValidationType = "greater";
        greaterRule.ValidationParameters.Add("other", this.OtherControlName);
        yield return greaterRule;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var otherPropertyInfo = validationContext.ObjectType.GetProperty(this.OtherControlName);
        var otherDate = (DateTime)otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
        var thisDate = (DateTime)value;

        if (thisDate <= otherDate)
        {
            var message = this.FormatErrorMessage(validationContext.DisplayName);
            return new ValidationResult(message);
        }

        return null;
    }
}

}

在创建此属性以便在客户端进行此验证后,我已向 JQuery 不显眼的验证器添加了一个方法,如下所示。

 jQuery.validator.addMethod("greater", function (value, element, param) {
    return Date.parse(value) > Date.parse($(param).val());
 });

 jQuery.validator.unobtrusive.adapters.add("greater", ["other"], function (options) {
    options.rules["greater"] = "#" + options.params.other;
    options.messages["greater"] = options.message;
 });

 jQuery.validator.addMethod('greaterThanDate', function (value, element, params) {
    var date = value.toString().split('/');
    var PassDay = date[0];
    var PassMonth = date[1];
    var passyear = date[2];
    if (PassDay != null && PassMonth != null && passyear != null) {
        var passDate = new Date(passyear, PassMonth - 1, PassDay);

        var currentdate = new Date();
        return passDate < currentdate;
    }
    return true;
 }, '');

 // and an unobtrusive adapter
 jQuery.validator.unobtrusive.adapters.add('futuredate', {}, function (options) {
    options.rules['greaterThanDate'] = true;
    options.messages['greaterThanDate'] = options.message;
 });

但是,除了当前稳定版本的 Chrome 之外,此验证在所有浏览器中都适用于我。我很困惑。谁能帮我解决这个问题?

【问题讨论】:

    标签: jquery asp.net-mvc asp.net-mvc-3 google-chrome


    【解决方案1】:

    我已经解决了这个问题,这只是在 chrome 中解析日期时的文化特定问题。我不清楚为什么文化问题只出现在 chrome 中而不出现在其他浏览器中。

    无论如何,我通过以下帖子的答案解决了这个问题

    Jquery Date.parse returning NaN in Chrome browser?

    【讨论】:

      【解决方案2】:

      你可以试试&lt;%: Html.JQueryDatePickerGlobalizationScript() %&gt;MVC Controls Toolkit

      Html.JQueryDatePickerGlobalizationScript() 将 DatePicker 的文化设置为当前 Thread 的 CurrentCulture,并使用 CDN 中的 Globalization/Localization 脚本:http://jquery-ui.googlecode.com/svn/trunk/ui/i18n/

      【讨论】:

        猜你喜欢
        • 2011-08-05
        • 2012-03-06
        • 2014-11-03
        • 2013-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-26
        相关资源
        最近更新 更多