如何使用kendo验证器验证日期格式为yyyy-MM-dd? [英] How to validate a date is in the format yyyy-MM-dd using kendo validator?
问题描述
我有一个kendo日期选择器,其构造如下:
I have a kendo date picker that is constructed as follows:
$("#date").kendoDatePicker({
format: "yyyy-MM-dd",
footer: " ",
parseFormats: ["MM/dd/yyyy", "dd/MM/yyyy"]
});
我想使用kendo验证器来验证日期是否包含有效日期,格式为yyyy-MM-dd。我已经尝试过:
I would like to use the kendo validator to validate that the date contains a valid date in the format of yyyy-MM-dd. I have tried this:
<input type="date" id="date" placeholder="yyyy-mm-dd" name="date" required data-required-msg="Please enter a date." data-date-msg="Please enter a valid date."/>
验证器正确验证必需条件时,似乎没有验证日期是正确的格式或是有效的日期。例如,abc作为有效日期通行为2013-18-85。如何使用验证器确保正确格式的有效日期?
While the validator does correctly validate the "required" condition, it does not seem to validate that the date is in the correct format or is a valid date. For instance, "abc" passes as a valid date as does 2013-18-85. How can I use the validator to ensure a valid date in the correct format?
推荐答案
如果您想要验证
a date
您需要定义规则(无内置规则)。
If you want to validate
a date
you need to define a rule (no built-in rule).
尝试定义:
$("#date").kendoValidator({
rules: {
date: function (input) {
var d = kendo.parseDate(input.val(), "yyyy-MM-dd");
return d instanceof Date;
}
}
});
注意:记住,KendoUI首先使用 parseFormats
选项来解析日期,然后将其转换为格式
选项,最后运行验证。这就是为什么我使用验证 yyyy-MM-dd
而不是 [MM / dd / yyyy,dd / MM / yyyy]
。
NOTE: Remember that KendoUI first uses parseFormats
option for parsing the date, then converts it to the format
option and finally run validations. That's why I use in validation yyyy-MM-dd
and not ["MM/dd/yyyy", "dd/MM/yyyy"]
.
这篇关于如何使用kendo验证器验证日期格式为yyyy-MM-dd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!