Forms

OTP

A one-time code field: one box per character, with the focus moving on as the user types, the whole code pasted at once, and the code filled in by the phone from an SMS. The value is the joined code, in a hidden field and in the bound property.

tk:otp builds the boxes from format; the inner tags are written only for a custom layout.

$verificationCode:
Blade

Six boxes for digits under the label. Typing a digit fills the box and moves to the next; letters are ignored. Pasting 123456 in any box fills them all. On phones the numeric keyboard opens and the code from an SMS is offered above it. $code receives the joined value, '123456'.

Format and characters #

format sets the number of boxes and what each accepts: 9 a digit, A a letter, * either, and - a separator between groups, drawn as a dash. Letters are shown and stored in upper case. charset sets the default format of six boxes: numeric, alpha or alphanumeric.

Blade

Submitting when complete #

With submit="auto", the form is submitted as soon as the last box is filled. Any other string is the name of a Livewire event dispatched with the code, for a #[On('pinEntered')] method. private masks the characters like a password.

Status: — (try 123456), PIN received: —
Blade

Texts, placeholder and size #

The field props work here: description, help, size, and a placeholder shown in every box.

We sent a code to [email protected].

The code expires in 10 minutes.

Blade

Events #

completed and incomplete fire on the tag with the code in $event.detail.value, here to enable the button only when the code is complete.

Blade

Custom layout #

Write tk:otp.group, tk:otp.input and tk:otp.separator in the slot for a layout of your own; label changes the separator.

Blade

Customization #

Classes go to the row of boxes and input:class to each box.

Blade

Reference

tk:otp
├── tk:otp.group
│   └── tk:otp.input ...
└── tk:otp.separator

tk:otp

Prop Type Default Description
format string six of the charset The boxes: 9 digit, A letter, * letter or digit, - separator between groups.
charset string numeric numeric, alpha or alphanumeric, for the default format and the keyboard.
private bool false Hides the characters, as a password.
submit string null auto submits the form when the code is complete; another string is a Livewire event dispatched with the code.

Also accepts the field props (label, description, help, placeholder...) and the field control props. placeholder is shown in every box (placeholder="0"). wire:model and x-model bind the code as a string.

Slots

Slot Description
default Boxes, groups and separators written by hand, in place of format.

Events

Event Detail When
changed { value } The user changed any box.
completed { value } Every box is filled.
incomplete { value } A change left some box empty.
cleared Every box is empty.
pasted { pasted, index } Text was pasted.
focused, blurred { input, index } A box gained or lost focus.

Prefixes

Prefix Goes to
hidden: The hidden input that holds the code.
input: Classes for each box (input:class="font-mono").

tk:otp.input, tk:otp.group and tk:otp.separator

One box (charset, private, inherited from the code), a group of boxes joined together, and the dash between groups (label changes it).

Events

Events are dispatched on the first box (focused and blurred on the box concerned), do not bubble, and are listened to on the tag.

With Alpine:

blade

Calling a Livewire action:

blade

Customization

Classes on the tag go to the row of boxes; other attributes go to each box. The parts around take the prefixes of the field props, and (listed in the Reference above).

blade

Accessibility

The boxes are a role="group" named by the label (or "One-time passcode"), and each box is named "Digit 1 of 6"... The first box has autocomplete="one-time-code", so browsers and phones offer the code received by SMS. Boxes written by hand get the same names and the autofill on the first box when they don't bring their own. The direction is always left to right.

Key Action
A character Fills the box and moves to the next.
Backspace Clears the box; on an empty box, moves to the previous one.
Arrow Left / Arrow Right Moves to the previous / next box.

Behavior notes

  • Focusing an empty box from outside the code moves the focus to the first empty one, so the code is typed in order; moving between boxes leaves it where it lands.
  • The automatic submit or event happens once per complete code: changing a character and completing it again fires it again.
  • A value set on the server fills the boxes.
  • The row has wire:ignore: a Livewire update does not reset what is being typed.
  • Resetting the form puts the boxes back to the code the field started with (bound to Livewire, the property's current value).