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.
On this page
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 init2. 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-otp3. Install example dependencies
These packages are imported directly by the examples. Some may already have been installed by the CLI.
npm install input-otpChoose 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.
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.
"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.
Usage 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.
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.
Enter the 6-digit code. You can paste the full code.
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.
0 of 4 digits entered.
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.
Enter the 6-digit code. You can paste the full code.
Alphanumeric recovery code
Allow letters and digits with REGEXP_ONLY_DIGITS_AND_CHARS and inputMode="text". The value keeps its original case.
Enter the 6-character recovery code.
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.
That code has expired. Request a new code.
Disabled code entry
Use disabled to block editing and focus while code entry is unavailable.
Code entry is currently unavailable.
Props & configuration
Everything you need to fine-tune this component.
| Prop | Type | Default | Description |
|---|---|---|---|
maxLengthRequired | number | — | Required maximum code length. Render one InputOTPSlot for each character with consecutive zero-based indices. |
value / defaultValue | string | — | 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. |
pattern | string | — | Use REGEXP_ONLY_DIGITS for numeric codes or REGEXP_ONLY_DIGITS_AND_CHARS for recovery codes, imported from input-otp. |
inputMode / autoComplete | native input attributes | — | Use numeric and one-time-code for numeric verification codes; use text for alphanumeric codes. |
id / aria-describedby / aria-invalid | native input attributes | — | Connect a real label and helper/error message. aria-invalid marks every slot with the error border. |
disabled | boolean | false | Disables the native input and dims the visual slots. |
containerClassName / className | string | — | 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. |
InputOTPGroup | component | — | Layout wrapper for adjacent slots. Use multiple groups with InputOTPSeparator for segmented codes. |
InputOTPSlot.indexRequired | number | — | Zero-based character index. Keep the index below maxLength. Slots are decorative; the native input provides the accessible control. |
InputOTPSeparator | component | — | Decorative separator between slot groups, hidden from assistive technology. |
