Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Textarea examples · Form

Textarea with Character Count for React

A textarea with a live count of its characters against a 200-character limit.

Framework
UI library

A limit people can't see is a limit they hit by surprise. This Textarea sets maxLength to 200 so the browser stops input there, and keeps its value in state so the FieldDescription under it can show the count, such as 42/200, on every keystroke.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

When should I add character counting to textareas?

When the field has a limit people need to write within: a post, a bio, an SMS, a meta description. Skip it for feedback or long-form fields with no practical limit, where a counter only adds noise.

Should maxLength prevent typing or just warn users?

maxLength is a hard stop: the browser won't take another character, and pasted text is cut at the limit. For a soft limit, leave maxLength off, let people type past it, and mark the field invalid with a FieldError until they edit it down. Use the hard stop when the limit is technical, such as a database column.

Should the character count change color as it approaches the limit?

It helps. The demo keeps the count muted; add a condition on the length, for example text-destructive once it reaches 90 percent of the limit, so people see the end coming. Keep the number itself, since color alone doesn't reach everyone.

Why place the character count on the right side?

The FieldDescription takes text-end, so the count sits at the end edge under the field, out of the reading path but easy to glance at, as on most social posting forms. text-end is logical, so it moves to the left in a right-to-left layout.

Does character counting need to update in real-time?

Yes. A count that changes as you type is what makes it useful, and counting a string's length on each change costs nothing. The Textarea is controlled: useState holds the value and an onChange handler updates it, so the count re-renders with each keystroke.

Should I show characters used or characters remaining?

The demo shows used out of the limit, which gives both numbers at once. A remaining count, such as 18 left, puts the emphasis on the end. Either works; use one format across your product.