Skip to content

OTP Input

Authentication UISince 0.3.0

Entry for one-time passcodes, PINs, MFA codes, and recovery codes. It provides:

  • numeric or alphanumeric codes of 1-32 characters, shown in visual cells;
  • paste distribution: a pasted code fills every cell, spaces and dashes are dropped;
  • autocomplete="one-time-code" for SMS and authenticator autofill;
  • overwrite typing, Backspace, and arrow-key navigation (native editing);
  • masking for PINs, visual grouping for recovery codes, optional upper-casing;
  • an aui-complete event and the FormControl contract.

Design choice: one input, not N inputs. One native <input> holds the whole code; the cells are aria-hidden presentation above it that let pointer events through. N single-character inputs break one-time-code autofill and password managers (they fill one field), need focus juggling that loses characters when typing fast, and make screen readers announce N unlabelled boxes. One input keeps paste, undo, selection, and editing native, and is announced once: “Verification code, 6 digits”.

  • One field, many cellsA single native input drawn as cells, so paste and autofill just work.
  • SMS autofillThe one-time-code hint lets phones fill codes from messages.
  • Numeric or alphanumericChoose the character set, uppercase letters, and the code length.
  • Grouped and maskedSplit long codes into groups and mask them like a password.
  • Complete eventFires once every cell is filled, ready to verify the code.

Use it for

  • SMS, email, or authenticator codes.
  • Numeric PINs (with mask).
  • Recovery codes (alphanumeric with groupSize).

Use something else for

  • Passwords: use Password Input.
  • Free-form identifiers of varying length: use Text Input.
Anatomy
<div class="aui-field aui-otp-input [--focused] [--invalid] [--disabled]">
<label class="aui-field__label" for="I">Verification code</label>
<div class="aui-field__description" id="D">help text</div>
<div class="aui-otp-input__control" dir="ltr"> (--aui-otp-length: 6)
<input id="I" class="aui-otp-input__input" inputmode="numeric" autocomplete="one-time-code" dir="ltr" aria-describedby="D H E">
<div class="aui-otp-input__cells" aria-hidden="true">
<span class="aui-otp-input__cell aui-otp-input__cell--filled">1</span> ... <span class="aui-otp-input__separator">-</span> ...
</div>
</div>
<span class="aui-visually-hidden" id="H">6 digits</span>
<div class="aui-field__error" id="E">...</div>
</div>

Use the bar above each frame to switch theme, density, direction, and locale.

OTP Input in the Sign-in and account security showcase

Section titled “OTP Input in the Sign-in and account security showcase”

The part of the Sign-in and account security showcase that uses OTP Input. Change its options, use it with the keyboard, and watch the event log; the link in the frame opens the full showcase. The bar above the frame switches theme, density, direction, and locale.

Showcase source
// Authentication showcase: sign-in with a one-time code, a password change with live strength
// requirements, and the session timeout warning. A simulated server checks the credentials.
import { duration, now } from '@absoluteui/core';
import { Button } from '@absoluteui/components/button';
import { InlineMessage } from '@absoluteui/components/inline-message';
import { OtpInput } from '@absoluteui/components/otp-input';
import { PasswordInput } from '@absoluteui/components/password-input';
import { PasswordStrengthMeter } from '@absoluteui/components/password-strength-meter';
import { SessionWarningDialog } from '@absoluteui/components/session-warning-dialog';
import { TextInput } from '@absoluteui/components/text-input';
import { createShowcase, hostIn, section, toggle, wait } from './kit.js';
const sc = createShowcase({
title: 'Sign-in and account security',
description: 'Sign in with the demo password, enter the code, change the password, and try the session timeout warning.',
simulated: true,
});
const DEMO_PASSWORD = 'correct horse battery';
const DEMO_CODE = '135790';
// Sign-in.
const signIn = section(sc, 'Sign in', ['password-input', 'otp-input']);
const signInForm = document.createElement('form');
signInForm.className = 'sc-form';
signInForm.noValidate = true;
signIn.append(signInForm);
const email = TextInput.create(hostIn(signInForm), { label: 'Email', type: 'email', value: 'ada@example.com', required: true, autocomplete: 'email' });
const password = PasswordInput.create(hostIn(signInForm), { label: 'Password', required: true, autocomplete: 'current-password', revealable: true, capsLockWarning: true, description: `Demo password: ${DEMO_PASSWORD}` });
const code = OtpInput.create(hostIn(signInForm), { label: 'One-time code', length: 6, type: 'numeric', groupSize: 3, autocomplete: 'one-time-code', description: `Sent to your phone. Demo code: ${DEMO_CODE}` });
code.host.hidden = true;
const messageHost = hostIn(signInForm);
const message = InlineMessage.create(messageHost, { tone: 'info', message: 'Not signed in.', live: 'polite' });
const submit = Button.create({ text: 'Continue', variant: 'primary', type: 'submit' }, document);
signInForm.append(submit.element);
let step: 'password' | 'code' | 'done' = 'password';
signInForm.addEventListener('submit', (e) => {
e.preventDefault();
void (async () => {
if (step === 'password') {
if (!email.reportValidity() || !password.reportValidity()) return;
submit.setLoading(true);
await wait(500);
submit.setLoading(false);
if (password.input.value !== DEMO_PASSWORD) {
password.setCustomError('The email or password is incorrect.');
password.reportValidity();
sc.log('sign-in rejected');
return;
}
password.setCustomError(null);
step = 'code';
code.host.hidden = false;
code.focus();
message.update({ tone: 'info', message: 'Enter the 6-digit code.' });
sc.log('password accepted');
} else if (step === 'code') {
await verify(code.getFormValue());
}
})();
});
const verify = async (value: string): Promise<void> => {
submit.setLoading(true);
await wait(400);
submit.setLoading(false);
if (value !== DEMO_CODE) {
code.setCustomError('That code is not valid. Try again.');
code.reportValidity();
sc.log('code rejected');
return;
}
code.setCustomError(null);
step = 'done';
message.update({ tone: 'success', message: 'Signed in.' });
sc.log('signed in');
};
code.host.addEventListener('aui-complete', () => {
if (step === 'code') void verify(code.getFormValue());
});
sc.listen(password.host, ['aui-caps-lock'], 'password');
sc.listen(code.host, ['aui-complete'], 'code');
// Password change.
const change = section(sc, 'Change password', ['password-input', 'password-strength-meter']);
const changeForm = hostIn(change);
changeForm.className = 'sc-form';
const next = PasswordInput.create(hostIn(changeForm), { label: 'New password', autocomplete: 'new-password', revealable: true, minLength: 12 });
const meter = PasswordStrengthMeter.create(hostIn(changeForm), {
label: 'Password strength',
input: next.input,
showRequirements: true,
requirements: [
{ id: 'length', label: 'At least 12 characters', test: (v) => v.length >= 12 },
{ id: 'mixed', label: 'Upper and lower case letters', test: (v) => /[a-z]/.test(v) && /[A-Z]/.test(v) },
{ id: 'digit', label: 'A digit', test: (v) => /[0-9]/.test(v) },
{ id: 'reuse', label: 'Not your current password', test: (v) => v !== '' && v !== DEMO_PASSWORD },
],
});
const confirm = PasswordInput.create(hostIn(changeForm), { label: 'Confirm new password', autocomplete: 'new-password', validate: (v) => (v === next.input.value ? null : 'The passwords do not match.') });
sc.listen(meter.host, ['aui-change'], 'strength');
// Session timeout.
const session = section(sc, 'Session timeout', ['session-warning-dialog']);
const sessionIntro = document.createElement('p');
sessionIntro.textContent = 'Start a session that ends in 25 seconds: the warning dialog opens 20 seconds before the end and offers to stay signed in or sign out.';
session.append(sessionIntro);
const sessionRow = hostIn(session);
sessionRow.className = 'sc-row';
const startButton = Button.create({ text: 'Session ends in 25 s', variant: 'secondary' }, document);
sessionRow.append(startButton.element);
let dialog: SessionWarningDialog | null = null;
const startSession = (seconds: number): void => {
dialog?.destroy();
dialog = SessionWarningDialog.create({ expiresAt: { kind: 'Instant', epochMilliseconds: now().epochMilliseconds + seconds * 1000 }, warnBefore: duration(Math.min(seconds, 20) * 1000) });
const d = dialog;
for (const type of ['aui-open', 'aui-extend', 'aui-sign-out', 'aui-expire'] as const) {
d.element.addEventListener(type, (e) => {
sc.log(`session ${type}`, e instanceof CustomEvent ? e.detail : undefined);
if (type === 'aui-extend') startSession(seconds);
});
}
};
startButton.element.addEventListener('click', () => {
startSession(25);
sc.log('session started (warning at 20 s left)');
});
toggle(sc, 'Mask the code', false, (mask) => {
code.update({ mask });
}, ['otp-input']);
toggle(sc, 'Disabled', false, (disabled) => {
for (const c of [email, password, code, next, confirm]) c.update({ disabled });
submit.update({ disabled });
}, ['password-input', 'otp-input']);

Six-digit MFA code that submits on completion

Section titled “Six-digit MFA code that submits on completion”
import { OtpInput } from '@absoluteui/components/otp-input';
const host = document.querySelector<HTMLElement>('#code');
const form = document.querySelector<HTMLFormElement>('#verify');
if (host && form) {
const otp = OtpInput.create(host, { label: 'Verification code', name: 'code', required: true, description: 'Enter the code from your authenticator app.' });
otp.host.addEventListener('aui-complete', () => {
form.requestSubmit();
});
}
import { OtpInput } from '@absoluteui/components/otp-input';
const host = document.querySelector<HTMLElement>('#recovery');
if (host) OtpInput.create(host, { label: 'Recovery code', length: 8, type: 'alphanumeric', uppercase: true, groupSize: 4, autocomplete: 'off' });
Terminal window
npm install @absoluteui/components @absoluteui/core @absoluteui/theme

Registry setup, the theme stylesheet, and the Content Security Policy are covered in Installation.

import { OtpInput } from '@absoluteui/components/otp-input';
import '@absoluteui/components/otp-input/style.css';
import { OtpInput } from '@absoluteui/components/otp-input';
import '@absoluteui/components/otp-input/style.css';
const host = document.querySelector<HTMLElement>('#pin');
if (host) OtpInput.create(host, { label: 'PIN', length: 4, mask: true, autocomplete: 'off' });
More usage
import { OtpInput } from '@absoluteui/components/otp-input';
const host = document.createElement('div');
document.body.append(host);
const otp = OtpInput.create(host, { label: 'Verification code', name: 'code', required: true });
otp.host.addEventListener('aui-complete', () => {
otp.host.closest('form')?.requestSubmit();
});

Using Angular? See OTP Input for Angular.

OtpInput.create(host: HTMLElement, options: OtpInputOptions): OtpInput

Global configuration key: otpInput (see Configuration).

Option Type Default Description
label string Visible label (required accessible name). Required.
hideLabel boolean false Visually hide the label.
length number 6 Number of characters (1..32).
type 'numeric' | 'alphanumeric' "numeric" Allowed characters.
mask boolean false Show dots instead of characters (PINs); the input becomes type=password.
uppercase boolean false Alphanumeric only: convert letters to upper case.
groupSize number Visual group size (separators are presentation only).
value string "" Initial value (normalized); reset() restores it.
name string Form field name.
description string Help text, referenced by aria-describedby.
required boolean false A code is required.
disabled boolean false Disabled.
readOnly boolean false Read-only.
autocomplete 'one-time-code' | 'off' "one-time-code" Autofill hint.
validate (value: string) => string | null Consumer validation of a complete code.
icons Partial<{ error: IconRef }> Icon slot overrides.
validationTiming 'blur' | 'input' | 'submit' "blur" When validation messages first appear; afterwards they update live. Globally configurable.
locale string "en-US" Message and number-formatting locale. Globally configurable. Inherited from common configuration.
Property Type Description
host HTMLElement Host element.
input HTMLInputElement The native input.
value string Current code.
complete boolean Every cell filled.
validity FieldValidity Current validity.
name string | null Form field name.
label string Accessible label (used by the error summary).
disabled boolean Whether the control is disabled.
dirty boolean Value differs from the initial value.
touched boolean Focus has left the control at least once.
options Readonly<OtpInputOptions> Current options.
Method Description
getFormValue(): string The code.
setValue(value: string): void Set the code (normalized, no events).
clear(): void Empty the code and focus the field (after a rejected code).
update(options: Partial<OtpInputOptions>): void Merge and apply options.
reportValidity(): boolean Show messages now (marks the control submitted) and return validity.
setCustomError(message: string | null): void External error (server, cross-field rule); null clears it.
focus(): void Move focus to the primary focusable element.
reset(): void Restore the initial value and clear dirty/touched/submitted state.
destroy(): void Remove rendering, registration, listeners, and the configuration subscription.
Event Detail Bubbles Cancelable Description
aui-input { validity: FieldValidity; complete: boolean } Yes No User edited the code (the code is not in the detail).
aui-change { validity: FieldValidity; complete: boolean } Yes No User committed the value (native change).
aui-complete { validity: FieldValidity } Yes No The code became complete through user input (typing, paste, or autofill).

All rules live in the aui.components cascade layer, so application styles override them without specificity battles.

Class Purpose
aui-otp-input Host (also aui-field).
aui-otp-input--focused | --invalid | --disabled State modifiers.
aui-otp-input__control Positioning container (dir=ltr); –aui-otp-length holds the length.
aui-otp-input__input The native input (transparent text, under the cells).
aui-otp-input__cells aria-hidden cell row.
aui-otp-input__cell One cell (–filled, –active).
aui-otp-input__separator Group separator.
Slot Default icon
error alert-circle

Tokens this stylesheet reads (verified in CI). Override them in a theme or with the Theme Designer.

--aui-border-width | --aui-color-border | --aui-color-border-strong | --aui-color-disabled-bg | --aui-color-error-fg | --aui-color-focus-ring | --aui-color-surface | --aui-color-surface-sunken | --aui-color-text | --aui-color-text-disabled | --aui-color-text-muted | --aui-control-height-lg | --aui-duration-fast | --aui-easing-standard | --aui-font-family-mono | --aui-font-family-sans | --aui-font-size-lg | --aui-font-size-md | --aui-font-size-sm | --aui-font-weight-medium | --aui-font-weight-semibold | --aui-icon-size-sm | --aui-line-height-tight | --aui-radius-md | --aui-space-1 | --aui-space-2

TypeScript definitions
type OtpInputType = 'numeric' | 'alphanumeric';
interface OtpInputConfig { validationTiming: ValidationTiming }
interface OtpInputOptions extends Partial<OtpInputConfig> {
label: string; hideLabel?: boolean; length?: number; type?: OtpInputType; mask?: boolean; uppercase?: boolean;
groupSize?: number; value?: string; name?: string; description?: string;
required?: boolean; disabled?: boolean; readOnly?: boolean; autocomplete?: 'one-time-code' | 'off';
validate?: (value: string) => string | null; locale?: string; icons?: Partial<OtpInputIcons>;
}
interface OtpInputValueDetail { validity: FieldValidity; complete: boolean }
interface OtpInputCompleteDetail { validity: FieldValidity }

Pattern: One native <input> with <label for>; visual cells are aria-hidden presentation. Chosen over N single-character inputs: one-time-code autofill and password managers fill one field, paste/undo/selection stay native, screen readers hear one labelled field with a length hint instead of N unlabelled boxes, and fast typing never loses characters to focus changes.

Checked on every docs build: the showcase passes axe (WCAG 2.2 A and AA rules) in the default, dark, and high-contrast themes and right to left; the playground loads without errors and passes axe before and after every on/off option is switched, with no Content Security Policy violations.

Key Context Action
Digits / letters Input focused Enter a character; in a full code, typing overwrites the next character.
Backspace / Delete Input focused Native deletion.
ArrowLeft / ArrowRight / Home / End Input focused Native caret movement; the active cell follows.
Ctrl/Cmd+V Input focused Paste; a full code (spaces and dashes removed) replaces the value.
Element Role
input textbox (native); none when masked (password)
cells none (aria-hidden=true)
Attribute Element Value
aria-describedby input Help text, the length hint (“6 digits”), then the visible error
aria-invalid input “true” only while an error message is shown
aria-hidden cells “true”

The active cell shows the focus indicator (color-focus-ring border and 1px ring) while the input has focus.

Criterion Level Note
1.3.1 Info and Relationships A
1.3.5 Identify Input Purpose AA
1.4.3 Contrast (Minimum) AA
1.4.10 Reflow AA
1.4.11 Non-text Contrast AA
2.1.1 Keyboard A
2.4.7 Focus Visible AA
3.3.1 Error Identification A
3.3.2 Labels or Instructions A
3.3.8 Accessible Authentication (Minimum) AA
4.1.2 Name, Role, Value A

Automated test results: Accessibility and compliance.

  • inputmode=numeric shows a numeric keypad for numeric codes.
  • WCAG 3.3.8: paste and autofill are supported, so users do not have to transcribe codes.
  • Manual screen-reader verification (NVDA, JAWS, VoiceOver) is pending.
Import cost Minified gzip brotli CSS brotli
@absoluteui/components/otp-input 41.6 kB 14.2 kB 12.7 kB 0.9 kB

Measured at build time, including every AbsoluteUI module the component pulls in. No third-party runtime dependencies.

Budget (enforced in CI) Limit
OtpInput.create, per instance (1,000 instances) < 1 ms average in headless Chromium

Tested scale: 1,000 inputs on one page in the performance test.

One input plus N spans; editing updates text of the cells only. Budget: OtpInput.create < 1 ms per instance (1,000 instances, e2e performance suite).

Measured run times: Performance.

States

empty, partially filled, complete, focused (active cell), masked, disabled, read-only, required, invalid.

State transitions
From Trigger To
empty / partial allowed character typed next cell filled; the active cell moves
partial last cell filled (typing, paste, autofill) complete; aui-complete
complete character typed with the caret inside the next character is overwritten; stays complete (no new aui-complete)
complete Backspace partial; the next completion emits aui-complete again
any paste of a full code value replaced (separators dropped)
Accessibility notes
  • inputmode="numeric" shows a numeric keypad for numeric codes.
  • WCAG 3.3.8: paste and autofill work, so users do not transcribe codes character by character.
  • If you submit on aui-complete, keep a visible submit button and show errors in the field so a mistyped code can be corrected.
  • Manual screen-reader verification is pending.
Responsive behavior

Cells share the available width (up to 3rem each); 6 cells fit a 320 CSS px viewport. The input font size is at least 16 px so mobile browsers do not zoom.

Right-to-left

Codes are left-to-right in every locale: the input and cells use dir="ltr". The label and messages follow the page direction.

Theming

Cells use the control surface tokens; the active cell uses color-focus-ring; invalid cells color-error-fg. Forced-colors mode: CanvasText borders, Highlight outline on the active cell.

Global configuration
configureAbsoluteUI({ components: { otpInput: { validationTiming: 'submit' } } });
Component configuration

Instance options override global and theme defaults. All options are dynamic.

Icons

icons.error (default alert-circle).

Security
  • Characters are filtered to the allowed set after NFKC normalization and rendered with textContent.
  • Events never include the code; the component never logs.
  • mask shows dots and switches the input to type="password".
  • Rate limiting, expiry, and verification of codes belong to your server; AbsoluteUI is not an authentication provider.
Data handling

The code lives only in the native input. No persistence, logging, network access, or telemetry.

Supported data scale

Codes of 1-32 characters.

Edge cases
  • Full-width digits (123) are accepted (NFKC).
  • Pasting “123 456” or “123-456” fills all cells; pasting a partial code inserts at the caret.
  • setValue() drops disallowed characters and truncates to length.
  • Changing length or type re-normalizes the current code.
Errors
Code Category Cause
OTP_INPUT_OPTIONS configuration Missing label, length outside 1-32, groupSize not less than length, uppercase without alphanumeric, invalid autocomplete.
CFG_INVALID_VALUE / CFG_UNKNOWN_OPTION configuration Invalid or unknown configuration.

Validity error codes: value-missing (“Enter the code.”), too-short (“Enter all 6 digits.”), custom.

Known limitations
  • ASCII letters and digits only.
  • Pointer-drag selection is not mirrored cell by cell.
  • Manual screen-reader verification is pending.
Unsupported scenarios
  • N separate inputs (deliberately not offered).
  • Custom character sets.