Component guideEditable source

Input OTP

A segmented one-time code field backed by one accessible input.

Original component docs
On this page4 sectionsBack to top

Use Input OTP for verification codes. Visible slots present one underlying input; do not replace them with separate controlled fields. Verification, expiry, and resend throttling belong in the application.

Live preview
Loading preview...

#Installation

Complete the base setup. For manual installation:

npx shadcn@latest add https://neobrutal-ui.andongmin.com/r/input-otp.json

#Usage

"use client";
import { useState } from "react";
import { REGEXP_ONLY_DIGITS } from "input-otp";
import { InputOTP, InputOTPGroup, InputOTPSlot } from "@/components/ui/input-otp";

export default function VerificationCode() {
  const [code, setCode] = useState("");
  return (
    <div className="grid gap-2">
      <label htmlFor="verification-code">Six-digit verification code</label>
      <InputOTP
        id="verification-code"
        maxLength={6}
        pattern={REGEXP_ONLY_DIGITS}
        value={code}
        onChange={setCode}
        autoComplete="one-time-code"
        inputMode="numeric"
      >
        <InputOTPGroup>
          {Array.from({ length: 6 }, (_, index) => (
            <InputOTPSlot key={index} index={index} />
          ))}
        </InputOTPGroup>
      </InputOTP>
      <p aria-live="polite">{code.length} of 6 digits entered.</p>
    </div>
  );
}

#API reference

Prop / partPurposeDefault
InputOTP.maxLengthAccepted character countRequired
InputOTP.value / onChangeControlled code stringUncontrolled
InputOTP.patternAllowed-character regex stringUpstream behavior
InputOTPGroupVisual slot groupFlex row
InputOTPSlot.indexZero-based slot indexRequired
InputOTPSeparatorDecorative group separatorDot

#Accessibility

Label the underlying input and explain the length. Set autocomplete and input mode appropriately and keep paste enabled. Associate verification errors with the field. Do not log or publish real authentication codes, and do not treat a complete-length value as proof that verification succeeded.