One-time code

Six digits, one value for the form

We sent a 6-digit code to your phone.

Know when the code is complete

Type or paste a code.

Letters and digits, a different length

Invalid and disabled

A smaller size

Behavior

  • Typing a character moves to the next box. A character that is not allowed is ignored.
  • A paste, or a code your phone offers from an SMS, is spread over the boxes. A whole code replaces what is there; a shorter one goes in from the box you are in.
  • The first box carries autocomplete="one-time-code", so a phone can offer the code from a message.
  • The hidden input named by data-name holds the joined value, so the form sends one field.

API

.otp is started on load, and so is one added to the page later. tsunami.otp.mount() starts one by hand. A form reset empties the boxes.

Methods

MethodDescription
tsunami.otp.mount(root?) Starts every .otp inside root (default: the document) that is not started yet.
tsunami.otp.value(el) The joined value of the .otp element, as a string.
tsunami.otp.set(el, text) Replaces the value. Characters that are not allowed are dropped and extra ones are cut.
tsunami.otp.clear(el) Empties the boxes and focuses the first.

Events

EventOnDescription
tsunami:otpcomplete the .otp, bubbles Sent when every box is filled. event.detail.value is the code.
input a box, bubbles The browser's own, sent as each box changes.

Keyboard

KeyDescription
Backspace Clears the box. In an empty box it goes back to the one before and clears that.
← → Moves to the previous or next box.
Home End Moves to the first or the last box.

Attributes

Attribute / elementValuesDescription
.otp on a div The row. Give it role="group" and an aria-label or aria-labelledby.
data-length a number (default 6) How many boxes the script adds. Leave it out when the boxes are in the markup.
data-type text (none = digits) Accepts letters and digits. The boxes get a text keyboard on a phone.
data-name a field name Adds the hidden input that holds the joined value.
data-size sm Smaller boxes.
data-disabled present or absent Disables the boxes the script adds. For boxes in the markup, put disabled on each.
aria-invalid true On .otp: the boxes get a red border.