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

后端 未结 2 2000
臣服心动
臣服心动 2021-01-01 01:50

I have a kendo date picker that is constructed as follows:

$(\"#date\").kendoDatePicker({
    format: \"yyyy-MM-dd\",
    footer: \" \",
    parseFormats: [\         


        
相关标签:
2条回答
  • 2021-01-01 02:20

    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

    Cheers

    0 讨论(0)
  • 2021-01-01 02:26

    If you want to validate a date you need to define a rule (no built-in rule).

    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. That's why I use in validation yyyy-MM-dd and not ["MM/dd/yyyy", "dd/MM/yyyy"].

    0 讨论(0)
提交回复
热议问题