# Character Limit

> Cap text length with a live character counter.

A shadcn/ui block from the [@flx](https://ui.flexnative.com) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/flx/rhf-rules-04).

- Page, with a live preview: https://shadcn.exchange/r/flx/rhf-rules-04
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/flx/rhf-rules-04-light-14fd5386b1e931e9.png
- Registry item: https://ui.flexnative.com/r/rhf-rules-04.json
- Listed: 2026-10-13, source last changed 2026-10-13

## Install

```bash
npx shadcn@latest add https://ui.flexnative.com/r/rhf-rules-04.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `@hookform/resolvers`, `lucide-react`, `react-hook-form`, `sonner`, `zod`
- Registry items: `card`, `field`, `textarea`, `button`

## Files

### `registry/forms/react-hook-form/rules/rhf-rules-04.tsx`

```tsx
'use client'

import { Controller, useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { PenLine } from 'lucide-react'
import { toast } from 'sonner'
import * as z from 'zod'

import { Button } from '@/components/ui/button'
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from '@/components/ui/card'
import {
  Field,
  FieldError,
  FieldGroup,
  FieldLabel,
} from '@/components/ui/field'
import { Textarea } from '@/components/ui/textarea'
import { cn } from '@/lib/utils'

const MIN = 10
const MAX = 160

const formSchema = z.object({
  bio: z
    .string()
    .min(MIN, `At least ${MIN} characters.`)
    .max(MAX, `No more than ${MAX} characters.`),
})

type FormValues = z.infer<typeof formSchema>

export function RhfRules04() {
  const form = useForm<FormValues>({
    resolver: zodResolver(formSchema),
    defaultValues: { bio: '' },
  })

  function onSubmit(data: FormValues) {
    toast.success('Bio saved', { description: `${data.bio.length} characters` })
  }

  return (
    <Card className="w-full max-w-sm">
      <CardHeader>
        <CardTitle className="flex items-center gap-2">
          <PenLine className="text-muted-foreground size-4" />
          Character limit
        </CardTitle>
        <CardDescription>Keep your bio short and to the point.</CardDescription>
      </CardHeader>
      <CardContent>
        <form onSubmit={form.handleSubmit(onSubmit)}>
          <FieldGroup>
            <Controller
              control={form.control}
              name="bio"
              render={({ field, fieldState }) => {
                const count = field.value.length
                const overLimit = count > MAX
                return (
                  <Field data-invalid={fieldState.invalid}>
                    <div className="flex items-center justify-between">
                      <FieldLabel htmlFor="rhf-rules-04-bio">Bio</FieldLabel>
                      <span
                        className={cn(
                          'text-muted-foreground text-xs tabular-nums',
                          overLimit && 'text-destructive',
                        )}
                      >
                        {count}/{MAX}
                      </span>
                    </div>
                    <Textarea
                      {...field}
                      id="rhf-rules-04-bio"
                      rows={3}
                      placeholder="Tell us a little about yourself…"
                      aria-invalid={fieldState.invalid}
                    />
                    {fieldState.invalid && (
                      <FieldError errors={[fieldState.error]} />
                    )}
                  </Field>
                )
              }}
            />
            <Button type="submit" size="sm">
              Save bio
            </Button>
          </FieldGroup>
        </form>
      </CardContent>
    </Card>
  )
}
```
