Input OTP

A one-time code or PIN entered in boxes. The boxes are drawn over one native input, so SMS autofill, paste and screen readers see a single field.

  • vig-input-otp
Import
import { InputOtp } from '@vigdx/ui/input-otp';

Basic

Bind [(value)] to a string signal. Typing fills the next box, and (completed) fires with the code once the last box is filled: verify it there. Pasting a whole code into any box fills them all.

6 digits

Enter the 6-digit code we sent to your phone.

0 of 6 digits entered.

PINs, groups and letters

length sets the number of boxes (6 by default). masked draws dots instead of the characters for PINs; the input stays a text field (so password managers leave it alone) and autocomplete defaults to off , since a PIN is not a code sent by SMS. groupSize draws a separator, e.g. 3 for 123–456. charset="alphanumeric" takes letters and digits and opens the upper-case text keyboard instead of the digit pad. Other characters are dropped, so a pasted 123-456 gives 123456 ; an alphanumeric code keeps its letters, so strip prefixes such as G- before passing it in.

4 digits

Four digits, hidden as you type.

6 digits

8 characters

4 digits

States and sizes

disabled , readonly and errors work as in the other fields; the boxes of an invalid field turn red and the message is linked to the input. size is sm , md (default) or lg ; the boxes scale with the font and shrink to fit narrow screens.

6 digits

6 digits

6 digits

This code has expired.

4 digits

Signal Forms

vig-input-otp implements FormValueControl<string> , so [formField] binds value, validation, required , disabled and readonly . The value is the code as typed: check its length with minLength . A value set from outside is cleaned up and written back ( 12a-456 becomes 12456 , a shorter length cuts it): Reactive Forms and ngModel controls stay pristine, but Signal Forms only takes values from a control as edits, so there the field turns dirty. Set clean values to avoid that.

6 digits

Valid: no

Reactive Forms

Import VigValueAccessor next to the component to use formControlName , [formControl] or ngModel .

4 digits

SMS autofill

  • The field is one <input autocomplete="one-time-code" inputmode="numeric">, the markup iOS Safari and Android Chrome need to offer the code from an SMS above the keyboard. Split inputs (one per digit) break that autofill, paste and screen readers, which is why the boxes are only drawn.
  • End the SMS with a line binding the code to your domain, @example.com #123456, so the browser only offers it on your site.
  • webOtp also asks the WebOTP API for the code where it exists (Chrome on Android): the browser reads the SMS and, once the user agrees, fills the field and fires (completed). The request stops when the field is destroyed, disabled or webOtp turns off.

Styling hooks

Internal elements carry a stable data-part and the component's data-scope; both are public API, so removing one is a breaking change. Select a part by both, e.g. [data-scope="input-otp"][data-part="label"] { … }: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through aria-* and, where listed, a data-state; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other data-* attributes listed below (such as data-opening while an overlay animates) are API too. See Customizing.

Scope: [data-scope="input-otp"]

InputOtp styling hooks
Part Element data-state
label The visible label, above the boxes. —
field The LTR row holding the boxes and the input. —
cells The boxes, hidden from assistive technology. —
cell One box: active under the caret or selection, else filled or empty . active, filled, empty
caret The caret drawn in the active empty box. —
separator The dash between groups ( groupSize ). —
input The native <input> over the boxes, holding the code. —
hint The vig-hint (a display: contents host; style its text part). —
disabled-reason The vig-hint with the disabled reason ( display: contents ). —
error The vig-field-error (a display: contents host; style its message part). —

Accessibility

  • Screen readers meet one text field named by its <label> and described by the expected length ("6 digits", from labels.lengthAriaLabel), then the hint and error. The boxes are hidden from assistive technology; the code reads as one value.
  • Keyboard: typing fills the next box and overwrites a selected one; Backspace removes the character before the caret (or the selected one); Arrow Left/Right, Home and End move between boxes, never past the first empty one. Shift+arrows and select-all work as in any text field.
  • The active box shows a 2px outline (Highlight in forced-colors mode) and an empty one a caret, which only blinks without prefers-reduced-motion.
  • With masked, the boxes show dots but screen readers still read the code, as they do a revealed password: the field never becomes a password input, which would invite password managers to save the PIN.
  • IME input (full-width digits from a Japanese keyboard, Arabic-Indic digits) is accepted and folded to ASCII when the composition ends.
  • The boxes always run left to right, also on RTL pages: codes are read in that order. The label, hint and error follow the page direction.

API

InputOtp

vig-input-otp component

A one-time code or PIN entered in boxes:

Example :
<vig-input-otp label="Verification code" (completed)="verify($event)" [(value)]="code" />

The boxes are drawn over ONE native <input> that holds the whole code, so SMS autofill (autocomplete="one-time-code" on iOS Safari and Android Chrome), paste, undo and screen readers all see a single labelled text field, never six. Typing fills the next box and overwrites a selected one; pasting a whole code from any box fills them all. (completed) fires when the last box is filled. The boxes always run left to right (dir="ltr"), also on RTL pages.

InputOtp props
Name Type Default Description
autocompletestring | undefinedundefined

Native autocomplete: one-time-code by default, so iOS and Android offer the code from an SMS; off when masked, since a PIN is not a code sent to the user.

charsetInputOtpCharset'numeric'

numeric (default: digits, the digit keypad) or alphanumeric (letters and digits, upper-case keyboard). Other characters are dropped, so a pasted 123-456 gives 123456; an alphanumeric code keeps letters, so strip prefixes such as G- yourself.

dirtybooleanfalse
—
disabledbooleanfalse
—
disabledReasonsreadonly WithOptionalFieldTree<DisabledReason>[][]
—
errorMessagesRecord<string, string>—
—
errorsreadonly ValidationError.WithOptionalFieldTree[][]
—
groupSizenumber0

Draws a separator after every this many boxes, e.g. 3 for 123–456; 0 for none.

hiddenbooleanfalse
—
hideLabelbooleanfalse

Visually hides the label, which still names the field.

hintstring | undefinedundefined
—
invalidbooleanfalse
—
label required string—
—
labelsPartial<InputOtpLabels>—

Texts (i18n); locale defaults.

lengthnumber6

Number of characters (boxes).

maskedbooleanfalse

Hides the characters behind dots, for PINs. The input stays a text field, so password managers leave it alone; screen readers still read the code, as with a revealed password.

namestring''
—
pendingbooleanfalse
—
readonlybooleanfalse
—
requiredbooleanfalse
—
sizeFieldSize'md'

sm, md (default) or lg: the boxes scale with it.

touchedbooleanfalse
—
value model string''
—
webOtpbooleanfalse

Also asks the WebOTP API for the code (Chrome on Android): the browser reads an SMS ending in @your.domain #123456 and, once the user agrees, fills the field and fires (completed). Feature-detected; elsewhere the one-time-code autofill applies. The request ends once the code is complete, however it was entered.

VigValueAccessor

directive

Bridges the vigdx form fields to Reactive Forms (formControl, formControlName) and template-driven forms (ngModel). Import it next to the component; Signal Forms ([formField]) need no accessor.

VigValueAccessor methods
Signature Description
registerOnChange(fn: (value: unknown) => void): void
—
registerOnTouched(fn: () => void): void
—
setDisabledState(disabled: boolean): void
—
writeValue(value: unknown): void
—

Are you sure?