# Adding a Maximum Length Validation to the Editor

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Editor</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>2024.4.1112</td>
 </tr>
</table>

## Description

How can I validate that the value of the Editor does not exceed a pre-defined maximum length?

## Solution

To validate the Editor value, use the [Kendo UI for jQuery Validator](https://docs.telerik.com/kendo-ui/controls/validator/overview) component with custom validation rules.

1. Set the  `data_maxtextlength` and `data_maxtextlength_msg` attributes through the [`HtmlAttributes`](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.ui.fluent/editorbuilder#htmlattributessystemobject) option of the Editor.
2. Extend the Validator with custom rules.

```Razor HtmlHelper
    <form>
        @(Html.Kendo().Editor()
            .Name("editor")
            .HtmlAttributes(new { data_maxtextlength="50", data_maxtextlength_msg="Text must be shorter than 50 chars" })
            .Value("Lorem ipsum dolor sit amet. Lorem ipsum dolor sit.")
        )

        <button class="k-button k-button-primary">Submit</button>
    </form>
```

```JS script
    <script>
        // Register custom validation rules.
        (function ($, kendo) {
            $.extend(true, kendo.ui.validator, {
                rules: {
                    maxTextLength: function (textarea) {
                        if (textarea.is("[data-maxtextlength-msg]") && textarea.val() != "") {
                            var maxlength = textarea.attr("data-maxtextlength");
                            var value = textarea.data("kendoEditor").value();
                            return value.replace(/<[^>]+>/g, "").length <= maxlength;
                        }

                        return true;
                    },
                    maxHtmlLength: function (textarea) {
                        if (textarea.is("[data-maxhtmllength-msg]") && textarea.val() != "") {
                            var maxlength = textarea.attr("data-maxhtmllength");
                            var value = textarea.data("kendoEditor").value();
                            return value.length <= maxlength;
                        }

                        return true;
                    }
                }
            });

            $("form").kendoValidator();
        })(jQuery, kendo);
    </script>
```

## More ASP.NET MVC Editor Resources

* [ASP.NET MVC Editor Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/editor/overview)

* [ASP.NET MVC Editor Demos](https://demos.telerik.com/aspnet-mvc/editor/index)

* [ASP.NET MVC Editor Product Page](https://www.telerik.com/aspnet-mvc/editor)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Editor for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/editor)

* [Server-Side API Reference of the Editor for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/editor)

* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2024-backwards-compatibility)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
