# Limit the Number of Lines in the TextArea

## Environment

| Product         | Progress® KendoReact TextArea |
| --------------- | ----------------------------- |
| Product Version | 16.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`](https://www.telerik.com/kendo-react-ui/components/inputs/api/textareaprops#rows) property sets the visible height of the TextArea, and the `maxLength` attribute, which the [Characters Counter](https://www.telerik.com/kendo-react-ui/components/inputs/textarea/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](https://www.telerik.com/kendo-react-ui/components/inputs/textarea/controlled_mode) and handle the [`onChange`](https://www.telerik.com/kendo-react-ui/components/inputs/api/textareaprops#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.

[Demo Source](https://demos.telerik.com/kendo-react-ui/knowledge-base/inputs/textarea-limit-number-of-lines/source.txt)

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

* [TextArea Controlled Mode](https://www.telerik.com/kendo-react-ui/components/inputs/textarea/controlled_mode)
* [TextArea Sizing](https://www.telerik.com/kendo-react-ui/components/inputs/textarea/sizing)
* [TextArea Characters Counter](https://www.telerik.com/kendo-react-ui/components/inputs/textarea/counter)
