This is a migrated thread and some comments may be shown as answers.

DatePicker format MM.yyyy mvcvalidate not a date

2 Answers 852 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
Konstantin
Top achievements
Rank 1
Konstantin asked on 02 Aug 2019, 06:16 AM

I am use DatePicker with format 'MM.yyyy' in asp net mvc.

Validation for mvc not use attribute data-format, and validate failed.

Exemple https://dojo.telerik.com/eZUTEbIP

In sources kendo.validation.js use: return kendo.parseDate(input.val(), input.attr(kendo.attr('format'))) !== null;

In sources kendo.aspnetmvc.js use: return input.val() === '' || kendo.parseDate(input.val()) !== null;

if change to same: return input.val() === '' || kendo.parseDate(input.val(), input.attr(kendo.attr('format'))) !== null; then validate success.

Please fix it in next release.

2 Answers, 1 is accepted

Sort by
0
Teya
Telerik team
answered on 05 Aug 2019, 12:41 PM
Hi Konstantin,

The Kendo UI MVC validator uses the current Kendo UI culture and based on it, determines whether a value is in a valid format or not. This is made on purpose, to make the validation more intuitive. The valid formats for the en-US culture (which is the default one) can be seen in the patterns object here:

https://github.com/telerik/kendo-ui-core/blob/master/src/cultures/kendo.culture.en-US.js

The jQuery validation, on the other hand, does not support globalized dates and numbers, which is why in kendo.validator.js we are passing the format data attribute of the date input to the parseDate method. You can read more about these limitations here:

https://docs.telerik.com/aspnet-mvc/troubleshoot/troubleshooting-validation#globalized-dates-and-numbers-are-not-recognized-as-valid-when-using-the-validator

I can suggest you two workarounds in this case:

1. If you are using the en-US culture, you can choose among the supported date patterns, e.g MMMM yyyy. I have updated the Dojo with the suggested approach:

https://dojo.telerik.com/eZUTEbIP/11

Alternatively, you can change the default en-US culture to one that supports the desired format: MM.yyyy

2. You can override the validation rule by adding the following script:

<script>
    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.val() === "" || kendo.parseDate(input.val(), "MM.yyyy") !== null;
    }
</script>

Please, check the following Dojo for a reference:

https://dojo.telerik.com/eZUTEbIP/12


I hope this helps.


Regards,
Teya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.

0
Konstantin
Top achievements
Rank 1
answered on 06 Aug 2019, 11:14 AM

Hi,

Thanks. I override the validation rule:

(function($, kendo) {
    $.extend(true,
        kendo.ui.validator,
        {
            rules: {
                mvcdate: function(input) {
                    if (input.is("[data-val-date]"))
                        return input.val() === '' || kendo.parseDate(input.val(), input.attr(kendo.attr('format'))) !== null;
                    return true;
                }
            }
        });
})(jQuery, kendo);

Tags
Date/Time Pickers
Asked by
Konstantin
Top achievements
Rank 1
Answers by
Teya
Telerik team
Konstantin
Top achievements
Rank 1
Share this question
or