Create

Form

Input OTP

Copy page View as Markdown Open in ChatGPT Open in Claude

Accessible one-time password component with copy paste functionality.

Usage#

app.component.ts
import { WDS_INPUT_OTP } from '@wds/angular/input-otp';

Examples#

Default#

The slots only show the characters: underneath is one native field, so pasting the code and SMS autofill work.

Separator#

Four digits#

maxlength sets the length of the code.

Alphanumeric#

pattern is the regular expression of one character; inputmode="text" shows a letter keyboard on phones.

Pattern#

A character that does not match pattern (default [0-9]) is not typed. @wds/core/input-otp exports REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS and REGEXP_ONLY_DIGITS_AND_CHARS.

Digits only

Controlled#

wds-input reports every change; wds-complete fires when every slot is filled.

Enter your one-time password.

Disabled#

Invalid#

invalid on wds-input-otp marks the whole code; invalid on a wds-input-otp-slot marks one slot.

Forms#

Integration with Angular forms. Pick the API your app uses.

Signal forms#

WdsInputOtpField connects the code to [formField]. minLength() checks that every digit is entered.

Verification codeEnter the 6-digit code sent to your email.Verify

Submitted: —

Template-driven#

WdsTextValueAccessor makes ngModel work on wds-input-otp, with required and minlength validators.

Verification codeEnter the 6-digit code sent to your email.Verify

Submitted: —

Reactive forms#

The same WdsTextValueAccessor works with formControl and formControlName.

Verification codeEnter the 6-digit code sent to your email.Verify

Submitted: —

API References#

wds-input-otp#

One-time code (OTP): a row of one-character slots. Underneath is a single native field (autocomplete="one-time-code"), so code autofill from SMS, pasting the whole code, Ctrl+A, copying and screen readers work (one field with a label). wds-input-otp-slot elements only show the characters and the caret. Without them the element renders maxlength slots itself. In a form it works like <input>: name, required (needs every character), reset.

Attributes

AttributeTypeDefaultDescription
valuestring''The code.
namestring''Field name in the form.
maxlength.maxLengthnumber | undefinedNumber of characters. Defaults to the number of wds-input-otp-slot elements, or 6.
patternREGEXP_ONLY_DIGITSPattern of one character (a regular expression), e.g. [0-9] or [a-zA-Z0-9].
inputmode'numeric' | 'text''numeric'On-screen keyboard: numeric for digits, text for letters.
sizeWdsInputOtpSize'default'Slot size.
disabledbooleanfalseDisables the field and excludes it from form submission.
readonlybooleanfalseThe code can be read and copied, but not changed.
requiredbooleanfalseEvery character is required for the form to be valid.
invalidbooleanfalseError state: red slots and aria-invalid.

Events

EventDescription
wds-inputCustomEvent<WdsTextChangeDetail>Every change of the value.
wds-completeCustomEvent<WdsTextChangeDetail>Every slot is filled.
wds-changeCustomEvent<WdsTextChangeDetail>A change committed when the field loses focus.

Methods

MethodDescription
formResetCallback()
formDisabledCallback(disabled: boolean)
formStateRestoreCallback(state: string | File | FormData | null)
checkValidity()Returns true when the value is valid; otherwise fires invalid on the element.
reportValidity()Like checkValidity(), and shows the browser validation bubble when the value is invalid.
setCustomValidity(message: string)Sets a custom validation error (an empty string clears it), like input.setCustomValidity().

Slots

SlotDescription
(default)wds-input-otp-group, wds-input-otp-slot, wds-input-otp-separator.

CSS parts (::part)

PartDescription
inputThe native (invisible) field.

wds-input-otp-group#

wds-input-otp-slot elements joined into one block. When a slot has an error, the whole group gets a red ring.

Slots

SlotDescription
(default)wds-input-otp-slot elements.

wds-input-otp-slot#

A one-character slot of wds-input-otp. The document order of the slots sets the character position. A click on a slot puts the caret there. The first and last slot of a group have rounded corners.

Attributes

AttributeTypeDefaultDescription
invalidbooleanfalseError of this slot only (invalid on wds-input-otp marks the whole code).

CSS parts (::part)

PartDescription
slotThe slot with the character.

wds-input-otp-separator#

A dash between groups of slots. Hidden from screen readers.

Accessibility#

Screen reader#

Screen readers get one native text field (autocomplete="one-time-code"), not one field per character. The slots and separators are hidden from screen readers.

Give the code a name: wds-field-label in wds-field, or aria-label on the element. Without it the field gets a default name with the code length (currently in Polish, Kod jednorazowy, 6 znaków).

invalid sets aria-invalid="true"; disabled, readonly and required are set on the native field. In a form the code is submitted under name.

Keyboard support#

KeyFunction
TabMoves focus to the first empty slot (or the last one when the code is full).
ArrowLeftArrowRightMoves between slots, up to the first empty one.
HomeEndMoves to the first slot or after the last character.
BackspaceDeletes the character before the caret.
Ctrl + ASelects the whole code (also ⌘ + A), e.g. to copy or replace it.
Ctrl + VPastes a code in place of the current one; characters outside pattern are skipped.
Footer