Inputs & controlsstable

    Input OTP

    One-time-code inputs with independent previews for numeric, controlled, grouped, alphanumeric, invalid, and disabled code entry.

    Follow the installation and quick start below to use Input OTP in your project, or read the complete setup guide.

    Accessible by designLight & darkCopy. Customize. Ship.
    On this page
    Live previewFluid

    Enter the 6-digit code. You can paste the full code.

    Interactive exampleReact / TypeScript / Tailwind CSS
    ADD TO YOUR PROJECT

    Installation

    1. Initialize once

    Run from an existing React project with Node.js 18 or later. Skip this step if the project already has u-lib.json. Initialization copies the shared stylesheet and adds its import to your application entry.

    npx @scriptwithahmad/u-lib@0.2.2 init

    2. Add Input OTP and example components

    This command includes the UI components used by this page's usage examples. The CLI copies editable source into your components directory and installs each component's dependencies. Existing source files are preserved.

    npx @scriptwithahmad/u-lib@0.2.2 add input-otp

    3. Install example dependencies

    These packages are imported directly by the examples. Some may already have been installed by the CLI.

    npm install input-otp

    Choose the tab for your package manager. Yarn commands require modern Yarn; Yarn Classic users can use npx. Use --dry-run to preview changes. See the installation guide for custom paths, JavaScript output and package-import alternatives.

    USE IN YOUR APP

    Quick start

    Copy this example into src/Example.tsx (or Example.tsx at the project root when you do not use src). Imports assume the default CLI paths. From a nested file such as src/app/page.tsx, adjust ./components to ../components. Keep the client directive in Next.js when using state or event handlers.

    input-otp-example.tsx
    "use client";
    
    import { useId } from "react";
    import { REGEXP_ONLY_DIGITS } from "input-otp";
    import { InputOTP, InputOTPGroup, InputOTPSlot } from "./components/ui/input-otp";
    
    export default function Example() {
      const id = useId();
      return <div className="w-full max-w-sm space-y-3">
        <label htmlFor={id} className="block text-sm font-medium">Verification code</label>
        <InputOTP id={id} maxLength={6} inputMode="numeric" pattern={REGEXP_ONLY_DIGITS} autoComplete="one-time-code" aria-describedby={`${id}-hint`}>
          <InputOTPGroup>{Array.from({ length: 6 }, (_, index) => <InputOTPSlot key={index} index={index} />)}</InputOTPGroup>
        </InputOTP>
        <p id={`${id}-hint`} className="text-xs text-muted-foreground">Enter the 6-digit code. You can paste the full code.</p>
      </div>;
    }
    

    Set maxLength and render a slot for each digit. Keep a visible label associated with the input and choose an appropriate validation pattern.

    Keep the shared stylesheet imported once. Replace sample image URLs and data with your own. Examples may use layout utilities beyond the precompiled component styles; compile added Tailwind classes in your app or replace them with your own CSS. Customize local source or className, and preserve accessible names and keyboard behavior.

    BUILD WITH IT

    Usage examples

    6 examples

    Try each example, then switch to Code to copy its complete implementation. These catalog snippets use the website's @/components/ui alias; replace it with your local generated path as shown in Quick start.

    01

    Six-digit verification

    Numeric code entry with an accessible label, numeric keyboard, and one-time-code autofill. Paste the full code or type digits; the underlying input handles selection and editing.

    Six-digit verification

    Enter the 6-digit code. You can paste the full code.

    02

    Controlled four-digit code

    Control value with onChange. The status reports progress; completion means the code is ready to verify, not that a server accepted it. onComplete is also available for your own callback.

    Controlled four-digit code

    0 of 4 digits entered.

    03

    Grouped verification code

    Use InputOTPSeparator to visually split the code into two groups. It remains one native input for paste, arrow keys, Backspace, and autofill.

    Grouped verification code

    Enter the 6-digit code. You can paste the full code.

    04

    Alphanumeric recovery code

    Allow letters and digits with REGEXP_ONLY_DIGITS_AND_CHARS and inputMode="text". The value keeps its original case.

    Alphanumeric recovery code

    Enter the 6-character recovery code.

    05

    Expired or invalid code

    Set aria-invalid and associate your error message. Code verification belongs to your application; this preview does not contact a server.

    Expired or invalid code

    That code has expired. Request a new code.

    06

    Disabled code entry

    Use disabled to block editing and focus while code entry is unavailable.

    Disabled code entry

    Code entry is currently unavailable.

    THE API

    Props & configuration

    Reference

    Everything you need to fine-tune this component.

    Props and configuration
    PropTypeDefaultDescription
    maxLengthRequirednumber—Required maximum code length. Render one InputOTPSlot for each character with consecutive zero-based indices.
    value / defaultValuestring—Controlled or initial code value. Pair value with onChange; defaultValue is uncontrolled.
    onChange(value: string) => void—Receives the complete code as a string when it changes. This is not a native change-event callback.
    onComplete(value: string) => void—Called when maxLength characters have been entered. Verify the code in your application; completion alone does not authenticate a user.
    patternstring—Use REGEXP_ONLY_DIGITS for numeric codes or REGEXP_ONLY_DIGITS_AND_CHARS for recovery codes, imported from input-otp.
    inputMode / autoCompletenative input attributes—Use numeric and one-time-code for numeric verification codes; use text for alphanumeric codes.
    id / aria-describedby / aria-invalidnative input attributes—Connect a real label and helper/error message. aria-invalid marks every slot with the error border.
    disabledbooleanfalseDisables the native input and dims the visual slots.
    containerClassName / classNamestring—Styles for the visible OTP container or the underlying native input. The component uses one native input for typing, paste, selection, arrow keys, and autofill.
    InputOTPGroupcomponent—Layout wrapper for adjacent slots. Use multiple groups with InputOTPSeparator for segmented codes.
    InputOTPSlot.indexRequirednumber—Zero-based character index. Keep the index below maxLength. Slots are decorative; the native input provides the accessible control.
    InputOTPSeparatorcomponent—Decorative separator between slot groups, hidden from assistive technology.