简体   繁体   English

如何验证日期的格式为yyyy-MM-dd,使用kendo验证器?

[英]How to validate a date is in the format yyyy-MM-dd using kendo validator?

I have a kendo date picker that is constructed as follows: 我有一个kendo日期选择器,构造如下:

$("#date").kendoDatePicker({
    format: "yyyy-MM-dd",
    footer: " ",
    parseFormats: ["MM/dd/yyyy", "dd/MM/yyyy"]
  });

I would like to use the kendo validator to validate that the date contains a valid date in the format of yyyy-MM-dd. 我想使用kendo验证器来验证日期是否包含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."/>

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. 例如,“abc”作为有效日期传递,如2013-18-85。 How can I use the validator to ensure a valid date in the correct format? 如何使用验证器确保格式正确的有效日期?

If you want to validate a date you need to define a rule (no built-in rule). 如果要validate date ,则需要定义规则(无内置规则)。

Try defining: 尝试定义:

$("#date").kendoValidator({
    rules: {
        date: function (input) {
            var d = kendo.parseDate(input.val(), "yyyy-MM-dd");
            return d instanceof Date;
        }
    }
});

NOTE: Remember that KendoUI first uses parseFormats option for parsing the date, then converts it to the format option and finally run validations. 注意:请记住,KendoUI首先使用parseFormats选项来解析日期,然后将其转换为format选项,最后运行验证。 That's why I use in validation yyyy-MM-dd and not ["MM/dd/yyyy", "dd/MM/yyyy"] . 这就是我在验证yyyy-MM-dd而不是["MM/dd/yyyy", "dd/MM/yyyy"]

The answer is: 答案是:

<script src="@Url.Content("~/Scripts/kendo/2015.2.805/kendo.aspnetmvc.min.js")"></script>
<script type="text/javascript">
    kendo.ui.validator.rules.mvcdate = function (input) {
        //use the custom date format here
        //kendo.parseDate - http://docs.telerik.com/kendo-ui/api/javascript/kendo#methods-parseDate
        return !input.is("[data-val-date]") || input.val() === "" || kendo.parseDate(input.val(), "@(MvcApplication.AppCulture.DateTimeFormat.ShortDatePattern)") !== null;
    };
</script>

Here is more information: http://docs.telerik.com/kendo-ui/aspnet-mvc/validation 以下是更多信息: http//docs.telerik.com/kendo-ui/aspnet-mvc/validation

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 jquery 验证这种格式 (yyyy-mm-dd) 的日期? - How do I validate a date in this format (yyyy-mm-dd) using jquery? 如何在 Kendo 中验证 dd/MM/yyyy 日期格式? - How to validate dd/MM/yyyy Date Format in Kendo? 在 WSO2 中验证 yyyy-MM-dd 格式的字符串日期 - Validate a String date with yyyy-MM-dd format in WSO2 如何将 ISO 日期转换为日期格式 yyyy-mm-dd? - How to convert an ISO date to the date format yyyy-mm-dd? 格式 JavaScript 日期为 yyyy-mm-dd - Format JavaScript date as yyyy-mm-dd 如何使用 NodeJS 将 UTC 日期格式化为“YYYY-MM-DD hh:mm:ss”字符串? - How to format a UTC date as a `YYYY-MM-DD hh:mm:ss` string using NodeJS? 使用JavaScript在pentaho中将日期格式从“ yyyy-mm-dd”转换为“ dd / mm / yyyy” - Date format conversion from “yyyy-mm-dd” to “dd/mm/yyyy” in pentaho using javascript 如何获取 YYYY-MM-DD 格式的日期? - How do I get a date in YYYY-MM-DD format? 如何用点将日期19990813(YYYY-MM-DD)格式化为13.08.1999(DD-MM-YYYY)? - How to format the date 19990813 (YYYY-MM-DD) to 13.08.1999 (DD-MM-YYYY) with the the dots? 将“ yyyy-mm-dd”日期格式设置为“ dd month yyyy” - Format a “yyyy-mm-dd” date into “dd month yyyy”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM