Disable some Kendo messages and show default instead

3 posts, 0 answers
  1. Volker
    Volker avatar
    22 posts
    Member since:
    Jul 2013

    Posted 17 Feb 2014 Link to this post

    Hi,

    I am using Kendo Validation throughout my ASP.NET MVC project. I have a problem with email-validation which cost me all the evening:

    I use this attribute for my C# property
    [EmailAddress(ErrorMessageResourceType=typeof(Resources.PersonResources), ErrorMessageResourceName = "MailAddressInvalid", ErrorMessage = null)]
    public string MailAddress { get; set; }


    Without Kendo validation, this leads to the following: On validating the Mailaddress, the error message which I defined in the resource file is shown. 

    Now with Kendo: It seems Kendo overrides this custom message with a Kendo one, something like "<HTML element id> is not a valid mailaddress".

    But I need the one I defined in the resource! So, is a way to disable the Kendo default messages for inputs of type email? I know I could do some custom code to get the message from the input element (something like that could work), but I really hope there is a more intelligent way...

    Thanks!
  2. Volker
    Volker avatar
    22 posts
    Member since:
    Jul 2013

    Posted 17 Feb 2014 in reply to Volker Link to this post

    Answer to myself - this does the trick:

    var validator = $(this).kendoValidator(
                    {
                        validateOnBlur: false,
                        messages: {
                            email: function (input) {
                                return input.attr("data-val-email");
                            }
                        }
                    }).data("kendoValidator");
  3. Kendo UI is VS 2017 Ready
  4. Loren
    Loren avatar
    9 posts
    Member since:
    Dec 2013

    Posted 16 Jul 2014 Link to this post

    I know this is a bit old. But i just spend sometime trying to make this work. Below is a more Global way so you dont have to set that custom rule up each time you want to validate an email in your system. Also I put in a fall-back in case the [EmailAddress] DataAnnotation is missed.

    (function ($, kendo) {
        var dateValidation = function (input) {
            if (input.filter("[data-val-date]").length > 0) {
                return input.val() === "" || kendo.parseDate(input.val(), ["MMddyyyy", "MMddyy", "MM/dd/yyyy", "MM/dd/yy"]) !== null;
            }
            return true;
        };
     
        $.extend(true, kendo.ui.validator, {
            rules: {
                date: dateValidation,
                mvcdate: dateValidation
            },
            messages: {
                email: function (input) {
                    if (input.attr("data-val-email")) {
                        return input.attr("data-val-email");
                    }
                    else {
                        return "{0} is not valid email";
                    }
                }
            }
        });
    })(jQuery, kendo);
Back to Top