Different set of custom validation for gridview add new and update

2 posts, 0 answers
  1. lucerias
    lucerias avatar
    16 posts
    Member since:
    Nov 2011

    Posted 19 May 2016 Link to this post

    I have currently added the following custom validation to my interest name, but interest name has to be unique, so when user add new and existing interest name is already there, it will throw error. However, when user try to edit the record, it should be able to detect whether current interest name is changed, if it is a new interset name then only check only uniqueness validation, otherwise, it should allow the update to go through successfully.

    With the following validation, when i try to perform update, it also throw the validation error, and i am do not know how to make this validation valid for Add New all the time, and valid for Update only when interest name is changed.

        (function ($, kendo) {
            $.extend(true, kendo.ui.validator, {
                rules: { // custom rules
                    InterestNamevalidation: function (input, params) {
                        if (input.is("[name='InterestName']") && input.val() != "") {
                            var dataSource = $("#Customer").data("kendoGrid").dataSource;
                            var data = dataSource.data();

                            for (var i = 1; i < data.length; i++) {
                                if ($.trim(input.val().toLowerCase()) == $.trim(data[i].InterestName.toLowerCase())) {
                                    input.attr("data-InterestNamevalidation-msg", "InterestName should be unique");
                                    return false;

                        return true;
        })(jQuery, kendo);

  2. Georgi Krustev
    Georgi Krustev avatar
    3747 posts

    Posted 23 May 2016 Link to this post

    Hello lucerias,

    I would suggest you enhance your validation condition and check whether the model is new. When it is new, you are adding new item and you can continue with the check. If it is not new, then it is an Update action and you can skip this custom validation:
    if ($.trim(input.val().toLowerCase()) == $.trim(data[i].InterestName.toLowerCase())) {
    if (data[i].isNew()) { return true; }
    //it is new and we need to throw the error
    input.attr("data-InterestNamevalidation-msg", "InterestName should be unique");
    return false;

    Georgi Krustev
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top