8

我有以下控制:

@(Html.Kendo().DatePickerFor(model => model.Attributes.DueDate)
    .HtmlAttributes(new { 
        ID = "idSurvey_DueDate", 
        @data_bind = "value: DueDate", 
        @Class = "report-label datepicker surveyAttributesData", 
        TabIndex = 3 })
    .Min(DateTime.Now)                                                            
)

和下面的jQuery:

$("#idSurvey_DueDate").kendoValidator({
    rules: {
        dateValidation: function (e) {
            var currentDate = kendo.parseDate($(e).val());
            // Check if date parse was successful
            if (!currentDate) {
                return false;
            }
            return true;
        }
    },
    messages: {
        dateValidation: "Invalid Date!",
        min: "Date must not be in the past!"
    }
});

当我对此进行测试并输入无效日期时,我收到的消息不是我所期望的。相反,它是“字段 DueDate 必须是日期”。这个神秘消息来自哪里,为什么不使用我放入验证器的 messages 属性?我想要的只是不允许无效的日期格式,并且日期不是过去。所以必须执行最低限度。

4

3 回答 3

8

这段代码似乎工作正常:

$("form").kendoValidator({
  rules: {
    dateValidation: function(element) {
      var value = $(element).val();

      var date = kendo.parseDate(value);
      if (!date) {
        return false;
      }

      return true;
    },
    minDate: function(element) {
      var value = $(element).val();

      var date = kendo.parseDate(value);

      var result = date >= new Date();

      return result;
    }
  },
  messages: {
    dateValidation: "You must enter a date",
    minDate: "The date must not be in the past"
  }
});

这是一个现场演示:http: //jsbin.com/EvoroRe/1/edit

于 2014-01-11T07:39:04.387 回答
0

我建议添加mvcdate规则:

rules: {
    mvcdate: function (input) {
        var datarole = $(input).data('role');
        if (datarole === 'datepicker') {
            var value = $(input).val();
            if (value) {
                var date = kendo.parseDate(value, 'ddd, MMM d');
                if (!date) {
                    return false;
                }
            }
        }

        return true;
    }
},
messages: {
    mvcdate: function (intput) {
        return intput.attr('data-val-date');
    }
}

不幸的是dateValidation,规则的优先级较低,date只是mvcdate因为它们是默认的,也不是自定义的。据我了解,该mvcdate规则具有最高优先级,因为:

  • dateValidation某些控件已跳过规则,我收到“必须是日期”错误
  • date规则已通过,结果为 TRUE,但我仍然收到“必须是日期”错误
  • mvcdate规则帮助了我一个人。

您始终可以查看控制台中的 kendoValidator:

kendoValidator 调试器

于 2015-06-18T00:53:03.707 回答
0

I'm not sure if the kendo validator changed since the accepted answer, but you'll want to filter out and only apply date validation to datepicker inputs. Otherwise a textbox or other input will generate an error message about an invalid date. The rules should look like

$("#modForm").kendoValidator({
    rules: {
        dateValidation: function (input) {
            if (input.is('[data-role="datepicker"]')) {
                var value = $(input).val();

                var date = kendo.parseDate(value);
                if (!date) {
                    return false;
                }
            }
            return true;
        }
    },
    messages: {
        dateValidation: "You must enter a date",
    }
});
于 2019-07-29T16:21:05.853 回答