Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

$("#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. 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. How can I use the validator to ensure a valid date in the correct format?

like image 962
James Avatar asked Jan 02 '13 18:01

James


People also ask

How to validate date in kendo DatePicker?

To validate the input value of the DatePicker, use a client-validation framework such as the Kendo UI Validator. In this way, you can provide an error message to end users which prompts them to do the right actions for them to resolve the issue. For more details, refer to the demo on custom validation.

How do I change date format in kendo DatePicker?

You can control the format of the DatePicker by using the format property. The component can be configured either to display a single format at all times, or to display the value in different formats when the input is focused or blurred.


1 Answers

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"].

like image 188
OnaBai Avatar answered Oct 03 '22 22:10

OnaBai