OTP input

A fixed-length code, one box per character, posted as one field.

Form control. It takes input in a form, so it is drawn on the Every input type block, beside every other control, as it starts and with an error where it has one.

Anatomy

Verification code                  ← label, names the group, targets box 1
┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐
│ 4│ │ 8│ │ 2│ │  │ │  │ │  │      ← one box per character, field recipe "box"
└──┘ └──┘ └──┘ └──┘ └──┘ └──┘
We sent a six-digit code.          ← help, or the error in its place
[hidden input name="code" value="482"]
  • Boxes use the field recipe (Support\Field), the same shell and control as ds::input, with the box padding: md's height, no side padding, the character centred in tabular-nums. 44px wide below sm, 36px above.
  • The row never wraps. flex with min-w-0 and size="1" lets six boxes narrow to fit 320px. A code broken over two lines reads as two codes.
  • One hidden input carries the joined code and every host attribute but class and the bindings, so @complete and required sit on the thing that posts. The boxes have no name.
  • wire:model* and x-model* land on the root, beside x-modelable="dsValue", so they bind both ways (see Binding).
  • PHP renders value into the boxes and the hidden input, so a code survives a failed round trip and posts without the script.

States

StateTreatment
empty, filled, focus, hoverAs ds::input, per box
errorEvery box gets the error border and aria-invalid; the message replaces help
disabledEvery box and the hidden input disabled; the recipe's flat shell
maskedtype="password" boxes: dots, and a screen reader does not read the PIN

Behaviour (dsOtp)

InputResult
Typing an allowed characterFills the box, focus moves to the next
A character not allowed (letter in numeric)Ignored; the box keeps what it had
Typing into a filled boxReplaces its character (focus selects the box's content)
Backspace on a filled boxClears it
Backspace on an empty boxClears the previous box and moves there
Left / RightPrevious / next box
Home / EndFirst / last box
Paste, or autofill of several charactersSpread across the boxes, disallowed characters dropped; a full-length code fills from box 1, a shorter one from the current box; focus lands on the last box filled

Each change sets the hidden input and dispatches input and change on it. When every box is filled and the code differs from the last complete one, the hidden input dispatches a bubbling complete CustomEvent with detail: { value }.

No maxlength on the boxes: the browser's one-time-code autofill writes the whole code into box 1, and maxlength="1" would cut it to one character before the script could spread it.

Binding

x-modelable is Alpine's core directive for a component that acts as a form control. dsValue is the scope's one exposed name: the joined code, set by the behaviour on every change the reader makes, and watched, so a code the host sets is written into the boxes. A host value is filtered and cut to the length like a paste, and the normalised code is handed back, so the host's state and the boxes never disagree. A host change dispatches no input, change or complete, the way setting a field's value from script fires nothing. On first start the host's value wins over the server-rendered value. The name is prefixed because the host's expression on the root is read in this scope: an unprefixed value would shadow a host property called value.

Accessibility

Rolegroup around the boxes, labelled by the label (aria-labelledby)
Box name"Digit n of N", or "Character n of N" when numeric is false
DescriptionEvery box is described by the error or help, so it is read wherever focus lands
Invalidaria-invalid="true" on every box
AutofillBox 1 only: autocomplete="one-time-code"; the rest off
Phone keyboardinputmode="numeric" when numeric; autocapitalise and autocorrect off
FocusTab enters the box with focus and leaves the group from the box it is in; autofocus puts focus on the first empty box

Each box is a Tab stop. A roving tabindex would make the group one stop, but someone tabbing back to fix one digit expects to land in it, and every OTP field people meet elsewhere tabs box by box.

Without the script

Since 30B, one real text field posts the code: named, autocomplete="one-time-code", inputmode="numeric" for a numeric code, maxlength and a pattern of the length, required when the host asks, in a field shell. Alpine hides its shell (x-bind:hidden) and shows the boxes' group, drawn hidden; the boxes are drawn disabled (a hidden control must not block the form, nor post) and enabled by an Alpine binding, and they take required, while the field is bound back to type="hidden", so its required and pattern no longer hold the form. dsOtp writes the joined code into the field, as it wrote the hidden input before. Until 30B the field was type="hidden" and nothing could be typed with no script.

Do not

  • Do not use it for a password or anything without a fixed length.
  • Do not auto-submit with no button. complete is for the happy path; a corrected code, or a page without the script, needs a button.
  • Do not trust the client filter. Validate length and characters on the server.
  • Do not go past eight boxes. A longer reference is one ordinary field.

From the package repo: edit this page on GitHub.

Was this page helpful?