.NET core MVC Kendo UI - How to write custom validation rule in kendo grid in inline edit mode

465 views Asked by At

I am working on a .NET 6 MVC project which is using Kendo UI. I have a kendo grid with Add and Edit functionality.

On my grid there are some dropdown list and date pickers. How do I write a custom validation rule to validate my dropdown list?

Can anybody provide me some reference? thank You !!

1

There are 1 answers

0
Mohammed On

Try this code. I worked on ASP.NET MVC.

Add MinLength in model.

[Required(ErrorMessage = "Please enter first name")]
[MinLength(5, ErrorMessage = "Minimum 5 characters required")]
[Display(Name = "First Name")]
public string FirstName { get; set; }

And on razor page

<div class="form-group">
    @Html.LabelFor(model => model.FirstName, new { @class = "form-label required" })
    @(Html.Kendo().TextBoxFor(model => model.FirstName)
        .Name("FirstName")
        .HtmlAttributes(new { @class = "form-control", autocomplete = "off", tabindex = "1" })
    )
    @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>

And on javascript

$(function () {
    $("#CreateForm").kendoValidator({
        rules: {
            minlength: function (input) {
                if (input.filter("[data-val-minlength]").length) {
                    var _minLength = parseInt(input.attr("data-val-minlength-min"));
                    if (input.val().length < _minLength)
                        return false;
                    return true;
                }
                return true;
            }
        },
        messages: {
            minlength: function (input) {
                return input.attr("data-val-minlength");
            }
        }
    });
});