# Number Range

> Keep a numeric input within a sensible range.

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

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

## Install

```bash
npx shadcn@latest add https://ui.flexnative.com/r/rhf-rules-03.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`, `input`, `button`

## Files

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

```tsx
'use client'

import { Controller, useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { Hash } 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,
  FieldDescription,
  FieldError,
  FieldGroup,
  FieldLabel,
} from '@/components/ui/field'
import { Input } from '@/components/ui/input'

const MIN = 1
const MAX = 99

const formSchema = z.object({
  quantity: z.coerce
    .number({ message: 'Enter a number.' })
    .int('Whole numbers only.')
    .min(MIN, `Minimum is ${MIN}.`)
    .max(MAX, `Maximum is ${MAX}.`),
})

type FormInput = z.input<typeof formSchema>
type FormValues = z.output<typeof formSchema>

export function RhfRules03() {
  const form = useForm<FormInput, unknown, FormValues>({
    resolver: zodResolver(formSchema),
    defaultValues: { quantity: 1 },
  })

  function onSubmit(data: FormValues) {
    toast.success('Quantity confirmed', {
      description: `${data.quantity} item(s)`,
    })
  }

  return (
    <Card className="w-full max-w-sm">
      <CardHeader>
        <CardTitle className="flex items-center gap-2">
          <Hash className="text-muted-foreground size-4" />
          Number range
        </CardTitle>
        <CardDescription>
          Tell us how many you&apos;d like to order.
        </CardDescription>
      </CardHeader>
      <CardContent>
        <form onSubmit={form.handleSubmit(onSubmit)}>
          <FieldGroup>
            <Controller
              control={form.control}
              name="quantity"
              render={({ field, fieldState }) => (
                <Field data-invalid={fieldState.invalid}>
                  <FieldLabel htmlFor="rhf-rules-03-quantity">
                    Quantity
                  </FieldLabel>
                  <Input
                    {...field}
                    value={field.value as number}
                    id="rhf-rules-03-quantity"
                    type="number"
                    inputMode="numeric"
                    min={MIN}
                    max={MAX}
                    aria-invalid={fieldState.invalid}
                  />
                  {fieldState.invalid ? (
                    <FieldError errors={[fieldState.error]} />
                  ) : (
                    <FieldDescription>
                      Pick a whole number between {MIN} and {MAX}.
                    </FieldDescription>
                  )}
                </Field>
              )}
            />
            <Button type="submit" size="sm">
              Add to cart
            </Button>
          </FieldGroup>
        </form>
      </CardContent>
    </Card>
  )
}
```
