DatePicker format MM.yyyy mvcvalidate not a date

3 posts, 0 answers
  1. Konstantin
    Konstantin avatar
    36 posts
    Member since:
    Mar 2018

    Posted 02 Aug Link to this post

    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. Missing user
    Missing user avatar

    Posted 05 Aug Link to this post

    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.

  3. Konstantin
    Konstantin avatar
    36 posts
    Member since:
    Mar 2018

    Posted 06 Aug in reply to Missing user Link to this post

    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);

Back to Top