shadcn.io is not affiliated with official shadcn/ui
Textarea examples · Form
Required Textarea for React
A required textarea with a red asterisk after its label.
Show which fields are required before people submit, not after. The Textarea gets the native required attribute, so the browser blocks an empty submit and screen readers announce it as required, and its FieldLabel ends in an asterisk in the text-destructive color for sighted users.
Preview
Installation
FAQ
When should I mark textarea fields as required?
Only when you can't process the form without the value: the message of a contact form, the body of a comment. Every extra required field is a reason to abandon the form, so leave the rest optional.
Is the red asterisk accessible for screen readers?
The asterisk is hidden from screen readers with aria-hidden, because the required attribute already makes them announce the field as required. The asterisk is only there for sighted users scanning the form.
The asterisk is a span inside the FieldLabel, and required goes on the Textarea itself.
How do I validate required textareas on submit?
The required attribute already stops an empty submit with the browser's own message. For your own message, check the value when the form submits, mark the Field invalid and show a FieldError under the Textarea, as the Textarea with Error example does.
Can I use different colors for the required indicator?
Yes: it's a class on the asterisk, so swap text-destructive for another theme color. Red is the convention people recognize, though, and whatever you choose, use the same indicator on every required field of the form.
Related
- Textarea with HintA textarea with an Optional hint at the
- Textarea with ErrorAn invalid textarea with a destructive
- Textarea with Helper TextA textarea with helper text under it that
- Required LabelA label with a destructive asterisk that
- Required Field Validation FormA name and an email field marked with
- Textarea with Character CountA textarea with a live count of its