0

下面是客户端验证不起作用的代码......我在这个论坛中搜索了几个问题并写了这个..

这是自定义验证属性“startDateAttribute”

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class StartDateAttribute : ValidationAttribute, IClientValidatable
    {
        public StartDateAttribute ()
        {
        }

        public override bool IsValid(object value)
        {   
            var date = (DateTime)value;
            if (date.Date >= DateTime.Now.Date)
            {
                return true;
            }
            return false;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            yield return new ModelClientValidationRule
            {
                ErrorMessage = this.ErrorMessage,
                ValidationType = "DateRange"
            };
        }
    }

    [CurrentDateAttribute(ErrorMessage = "select the correct date")]    
    public DateTime? StartDate { get; set; }

这是添加的 JQuery 代码

      jQuery.validator.addMethod('DateRange', function (value, element, params) {
     var d = new Date();         
     var currentDate = (d.getMonth()+1)  + "/"+d.getDate()+ "/" + d.getFullYear() ;
    return value >= currentDate;
});

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

1 回答 1

0

客户端验证的要求之一是 ValidationType 和适配器名称应该匹配并且应该是小写的。

将 ValidationType 和适配器名称更改为“日期范围”并检查

于 2012-04-25T07:53:38.577 回答