OTP input
Collect one-time passwords, PIN codes, and verification codes with a single accessible input rendered as separate digit slots.
Overview
OTP (One-Time Password) inputs are a common pattern for two-factor authentication, verification codes, and PIN entry. Bootstrap's OTP input is built around a single real text field that is visually rendered as separate digit slots. This keeps the control simple and fully accessible—screen readers, password managers, and SMS autofill all treat it like any other text input—while still giving you the familiar multi-box appearance.
- One accessible control: a single
<input>backs the whole field, so assistive technology announces one field, not one per digit - Browser autofill: supports
autocomplete="one-time-code"for SMS/email code autofill - Paste support: paste a full code—even a formatted one like
123-456—and the extra characters are stripped automatically - Click to edit: click any slot to jump to it; typing overwrites that digit in place instead of pushing the others along
- Keyboard navigation: all native text editing (typing, arrows, backspace, delete, selection) works out of the box
- Masking and types: optionally mask the value, and restrict input to numeric, alphanumeric, or alphabetic characters
OTP input is built on form controls and shares its sizing tokens.
How it works
Use OTPInput with a length prop to determine how many slots to render. The component positions the input over the visual slots and draws one slot per character. Give the input an accessible name with aria-label or an associated FormLabel.
<OTPInput
aria-label="Verification code"
length={6}
/>Connected inputs
Set connected to merge the slots into a single cohesive field with shared borders.
<OTPInput
aria-label="Verification code"
connected
length={6}
/>Four-digit PIN
Set length to control the number of slots—use fewer for shorter codes like a 4-digit PIN.
<OTPInput
aria-label="PIN"
connected
length={4}
/>With separator
Use groups with an array of group sizes to split the slots into groups separated by a visual divider—handy for codes like "123-456". The separator is decorative and never becomes part of the value.
<OTPInput
aria-label="Verification code"
groups={[
3,
3
]}
length={6}
/>Sizes
Set size="sm" or size="lg" to change the slot size.
<>
<OTPInput
aria-label="Small verification code"
length={6}
size="sm"
wrapperClassName="mb-3"
/>
<OTPInput
aria-label="Large verification code"
length={6}
size="lg"
/>
</>Alphanumeric and masking
By default only digits are accepted. Set type to alphanumeric or alpha to accept letters, and mask to obscure the entered value while keeping it a normal text field (no type="password", so the numeric keyboard and autofill still work).
<>
<OTPInput
aria-label="Alphanumeric code"
connected
length={6}
type="alphanumeric"
wrapperClassName="mb-3"
/>
<OTPInput
aria-label="Masked code"
connected
length={6}
mask
/>
</>Disabled
Add the disabled attribute to the input to prevent interaction.
<OTPInput
aria-label="Verification code"
connected
disabled
length={6}
/>Validation
Set validation="valid" or validation="invalid" to show validation states.
<>
<OTPInput
aria-label="Verification code"
connected
defaultValue="123456"
length={6}
validation="valid"
wrapperClassName="mb-3"
/>
<OTPInput
aria-label="Verification code"
connected
defaultValue="123"
length={6}
validation="invalid"
/>
</>With form label
Associate a FormLabel with the input and add help text with aria-describedby for better accessibility.
<Stack gap={2}>
<FormLabel htmlFor="otpCode">
Verification code
</FormLabel>
<OTPInput
aria-describedby="otpHelp"
id="otpCode"
length={6}
/>
<FormText id="otpHelp">
Enter the 6-digit code sent to your phone.
</FormText>
</Stack>Usage
OTPInput initializes and disposes the plugin automatically. Pass options as props. Use onValueChange for edits and onComplete when all slots are filled.
Options
Pass options directly as OTPInput props:
| Name | Type | Default | Description |
|---|---|---|---|
length | number | Required | Number of slots. |
type | string | 'numeric' | Allowed characters: numeric, alphanumeric, or alpha. Sets inputmode and pattern accordingly. |
mask | boolean | false | If true, slots display a dot (•) instead of the entered character. |
groups | array, null | null | Group sizes used to insert separators (e.g. [3, 3]). |
separator | string | '·' | Character rendered between groups. |
Accessibility
Because the control is a single text input, it exposes one accessible name, role, and value—so assistive technology announces it as one ordinary text field instead of one edit field per digit. The visual slots are decorative and marked aria-hidden, and no per-digit aria-labels are needed.
- Give the input a single accessible name with
aria-labelor an associatedFormLabel, and add context witharia-describedby. - The component sets
autocomplete="one-time-code"and an appropriateinputmodeso codes can be autofilled and the right mobile keyboard appears. This supports WCAG 1.3.5 Identify Input Purpose. - Copy, paste, and autofill are never blocked, which keeps the control compliant with WCAG 3.3.8 Accessible Authentication (Minimum)—a single input is also what password managers and SMS autofill target reliably.
- For the verification flow itself, give codes a generous expiry (WCAG 2.2.1 Timing Adjustable) and avoid forcing users to re-enter a code already provided in the same process (WCAG 3.3.7 Redundant Entry).
CSS
Variables
.example {
--bs-otp-size: var(--bs-btn-input-lg-min-height);
--bs-otp-font-size: var(--bs-btn-input-font-size);
--bs-otp-gap: 0.5rem;
--bs-otp-slot-fg: var(--bs-btn-input-fg);
--bs-otp-slot-bg: var(--bs-btn-input-bg);
--bs-otp-slot-border-width: var(--bs-border-width);
--bs-otp-slot-border-color: var(--bs-border-color);
--bs-otp-slot-border-radius: var(--bs-radius-5);
}