New to KendoReact? Start a free 30-day trial

Limit the Number of Lines in the TextArea

Updated on Nov 12, 2026

Environment

ProductProgress® KendoReact TextArea
Product Version16.2.0

Description

How can I prevent users from entering more than a specific number of lines, for example five, in the KendoReact TextArea?

This KB also answers the following questions:

  • Does the rows property of the TextArea limit the number of lines?
  • How do I restrict the line breaks that users type or paste in the TextArea?

Solution

The rows property sets the visible height of the TextArea, and the maxLength attribute, which the Characters Counter article demonstrates, limits the number of characters. Neither of them limits the number of lines.

To restrict the lines, use the TextArea in controlled mode and handle the onChange event. In the handler, split the new value on line breaks, and keep only the allowed number of lines.

The following example demonstrates the complete implementation. Type or paste more than five lines in the TextArea to confirm that the extra lines are removed.

Example
View Source
Loading ...

The solution consists of the following steps:

  1. Set the value and rows properties of the TextArea. Set rows to the maximum number of lines, so that the TextArea displays all allowed lines without a scrollbar.

    tsx
    <TextArea rows={maxLines} value={value} onChange={handleChange} />
  2. In the onChange handler, split the value on line breaks and save only the first allowed lines in the state.

    tsx
    const handleChange = (event: TextAreaChangeEvent) => {
        const lines = event.value.split(/\r\n|\r|\n/);
    
        setValue(lines.length > maxLines ? lines.slice(0, maxLines).join('\n') : event.value);
    };

This approach counts line breaks, not lines that wrap visually because of the width of the TextArea.

See Also

In this article
EnvironmentDescriptionSolutionSee Also
Not finding the help you need?
Contact Support