# OTP verification

> Six-digit verification code card with auto-advancing numeric inputs, clipboard paste support, arrow navigation and resend control.

- Kind: block · Category: Auth · Interactivity: script
- Install: `npx shadcn@latest add @haze/otp-verification`
- File: `src/components/haze/blocks/otp-verification.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/blocks/otp-verification

## Use when
- Building a standalone 6-digit email or SMS verification screen during sign-in
- Supporting full-code clipboard paste and Backspace/Arrow navigation across digit slots

## Avoid when
- You only need the bare OTP input primitive inside a larger custom form; use otp-input

## Alternatives
- [otp-input](https://hazeui.com/ui/otp-input.md)
- [auth-login](https://hazeui.com/blocks/auth-login.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` | "haze-otp-verification" | no | Unique prefix for section heading IDs. |
| email | `string` | "nina@tidewell.dev" | no | Masked or full destination address shown under the heading. |
| class | `string` |  | no | Extra classes on the outer section. |

## Accessibility
- Each numeric input carries an explicit aria-label (Digit 1 of 6 through Digit 6 of 6) inside a labelled group.
- Verification and resend actions announce feedback via a polite role="status" message.
- Key 0-9 / Backspace: Enter digits with automatic focus advance and retreat
- Key ArrowLeft / ArrowRight: Move directly between adjacent digit slots

## Agent prompts

### Install and use

Install the Haze UI block "OTP verification" with `npx shadcn@latest add @haze/otp-verification`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import OtpVerification from "@/components/haze/blocks/otp-verification.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/blocks/otp-verification.md

### Customize with real content

Adapt the Haze otp-verification block for our product. Replace the demo values of these props with our real content: id, email, class. Keep the layout, the heading level rules and these accessibility details: Each numeric input carries an explicit aria-label (Digit 1 of 6 through Digit 6 of 6) inside a labelled group. Verification and resend actions announce feedback via a polite role="status" message. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using otp-verification, check it fits: use it when building a standalone 6-digit email or sms verification screen during sign-in; supporting full-code clipboard paste and backspace/arrow navigation across digit slots. Avoid it when you only need the bare otp input primitive inside a larger custom form; use otp-input. Alternatives: @haze/otp-input, @haze/auth-login.

## Example requests that should lead here
- Add an OTP verification card with 6 auto-advancing digit slots, paste support and a resend code button

## Source

```astro
---
import { CheckCircle2, KeyRound, RotateCw } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";

interface Props {
  id?: string;
  email?: string;
  class?: string;
}

const { id = "haze-otp-verification", email = "nina@tidewell.dev", class: className } = Astro.props;
const slots = [0, 1, 2, 3, 4, 5];
---

<section aria-labelledby={`${id}-title`} data-haze-otp-block class={cn("bg-background px-4 py-10 sm:px-6 sm:py-14", className)}>
  <div class="mx-auto max-w-md rounded-2xl border border-border bg-card p-6 text-center sm:p-8">
    <span class="mx-auto grid size-11 place-items-center rounded-xl border border-frame bg-muted text-muted-foreground" aria-hidden="true">
      <KeyRound class="size-5" />
    </span>
    <h2 id={`${id}-title`} class="mt-4 text-xl/7 font-semibold tracking-tight text-foreground">Enter verification code</h2>
    <p class="mt-1.5 text-xs/5 text-muted-foreground">We sent a 6-digit code to <strong class="text-foreground">{email}</strong>. Paste or type below.</p>

    <form data-otp-form class="mt-6 space-y-5">
      <div class="flex justify-center gap-2" role="group" aria-label="6-digit verification code">
        {
          slots.map((i) => (
            <input
              type="text"
              inputmode="numeric"
              maxlength="1"
              data-otp-slot={String(i)}
              aria-label={`Digit ${i + 1} of 6`}
              class="h-12 w-10 rounded-xl border border-frame-input bg-field text-center font-mono text-lg font-semibold text-foreground transition-[color,background-color,border-color,box-shadow] duration-150 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/24 focus-visible:outline-hidden sm:w-11 transition-[color,background-color,border-color,box-shadow] duration-150 hover:border-frame-foreground"
            />
          ))
        }
      </div>

      <button type="submit" class="inline-flex h-10 w-full cursor-pointer items-center justify-center gap-1.5 rounded-lg bg-primary text-sm font-medium text-primary-foreground hover:opacity-90 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
        <CheckCircle2 class="size-4" aria-hidden="true" />
        Verify code
      </button>
    </form>

    <div class="mt-4 flex items-center justify-center gap-2 text-xs text-muted-foreground">
      <span>Did not receive it?</span>
      <button type="button" data-otp-resend class="inline-flex h-7 cursor-pointer items-center gap-1 rounded-md px-2 font-medium text-foreground hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
        <RotateCw class="size-3" aria-hidden="true" />
        Resend code
      </button>
    </div>

    <p data-otp-status role="status" class="mt-3 min-h-4 text-xs/4 text-muted-foreground"></p>
  </div>
</section>

<script>
  function initOtpBlocks() {
    document.querySelectorAll<HTMLElement>("[data-haze-otp-block]").forEach((root) => {
      if (root.hasAttribute("data-enhanced")) return;
      root.setAttribute("data-enhanced", "");
      const inputs = Array.from(root.querySelectorAll<HTMLInputElement>("[data-otp-slot]"));
      const status = root.querySelector<HTMLElement>("[data-otp-status]");

      inputs.forEach((inp, i) => {
        inp.addEventListener("input", () => {
          inp.value = inp.value.replace(/\D/g, "").slice(0, 1);
          if (inp.value && i < inputs.length - 1) inputs[i + 1].focus();
        });
        inp.addEventListener("keydown", (ev) => {
          if (ev.key === "Backspace" && !inp.value && i > 0) inputs[i - 1].focus();
          if (ev.key === "ArrowLeft" && i > 0) inputs[i - 1].focus();
          if (ev.key === "ArrowRight" && i < inputs.length - 1) inputs[i + 1].focus();
        });
        inp.addEventListener("paste", (ev) => {
          const digits = (ev.clipboardData?.getData("text") ?? "").replace(/\D/g, "").slice(0, 6);
          if (!digits) return;
          ev.preventDefault();
          digits.split("").forEach((d, idx) => { if (inputs[idx]) inputs[idx].value = d; });
          inputs[Math.min(digits.length, inputs.length - 1)].focus();
        });
      });

      root.querySelector<HTMLFormElement>("[data-otp-form]")?.addEventListener("submit", (ev) => {
        ev.preventDefault();
        const code = inputs.map((i) => i.value).join("");
        if (status) status.textContent = code.length === 6 ? `Verified code ${code}.` : "Enter all 6 digits to verify.";
      });

      root.querySelector<HTMLButtonElement>("[data-otp-resend]")?.addEventListener("click", () => {
        inputs.forEach((i) => (i.value = ""));
        inputs[0]?.focus();
        if (status) status.textContent = "Sent a fresh 6-digit verification code.";
      });
    });
  }
  initOtpBlocks();
  document.addEventListener("astro:page-load", initOtpBlocks);
</script>
```
