Overview
An OTP (one-time password) input is a <fieldset> with the nds-otp-group class. The user types a code in it that was sent by SMS, email or an authenticator app, such as in a two-factor sign-in. It holds a legend, one box for each digit, and a hidden input with the whole code for the form. A separator can split a long code into two sets.
Pick another component when:
Markup
Variants
For another code length, copy a box and give it a unique id. The script numbers every box’s aria-label again (“Digit 2 of 5”).
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | 4 digits (default) | — | — | A short code, such as an SMS code |
| Structure | 6 digits | canon #otp-6 |
— | A longer code, such as one from an authenticator app. A separator splits it into two sets of three |
| Size | SM | .nds-sm |
.nds-otp-group |
32px boxes with smaller digits, for a dense form or a small card |
| Size | MD (default) | — | — | 40px boxes |
| Size | LG | .nds-lg |
.nds-otp-group |
48px boxes with larger digits, 44px on phones, for a page that holds only the code |
| No separator | No separator | remove | .nds-otp-separator |
Six boxes in one set. Keep the separator for a code the user reads as two sets |
| Field states | Label, info, feedback, required | — | — | Shared by every form field. See Forms |
Built-in Features
-
Auto-initialization
Every
nds-otp-groupon the page starts on its own, and so does a group added later. The first group on a page that had none needsNDS.init.mount()on the new content. -
Auto-advance
A typed digit moves the focus to the next box. A box takes digits only, and a new digit replaces the one in the box. A click on an empty group moves the focus to the first box.
-
Keyboard Navigation
The left and right arrow keys move to the box on that side. Backspace empties the box and moves back. Delete empties it and moves forward.
-
Paste Support
A pasted or autofilled code fills the boxes from the box that has focus. Characters that are not digits are removed.
-
One Value for the Form
The hidden
nds-otp-valueinput holds the whole code after each change, so the form sends it as one field. -
Submit Check
In a form, a group with
data-requiredblocks the submit until every box holds a digit. The boxes turn red, and a message under them names the number of boxes: "Please enter all 4 digits". -
Error Clears on Input
A key press in any box removes the group's error and its message.
-
Autofocus
A box with
autofocusgets the focus when the script starts, even when the browser lost it while the page loaded. -
Accessibility
In high contrast mode, the box borders are 2px wide. With reduced motion, the boxes change color with no transition.
Best Practices
- Make one box for each digit of the code the server sends. The submit check counts the boxes.
- Put
autocomplete="one-time-code"on the first box only, so the phone offers the code from an SMS. - Give the name to the hidden
nds-otp-valueinput, not to the boxes. A named box sends its digit as one more field. - Keep the
aria-labelthe canon gives each box. The boxes have no visible label, so the script names each box’s place in the page’s language, such as “Digit 2 of 4”. A label of your own stays as you wrote it. - Keep
data-requiredon the group. Without it, the form does not check the code. - Check the code on the server. The OTP input checks only that every box is full.
- Put
autofocuson the first box only when the code is the one task on the page, such as a verification step. - After you send a new code, empty the boxes with
NDS.Otp.clear(), so the user cannot send the old code. - For the label, info text and feedback, see Forms. They work the same on every field.
- The field colors come from the form tokens: see Forms, Tokens.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-required |
.nds-otp-group |
A form submit checks that every box holds a digit. It also adds the required mark to the legend |
data-error-message |
.nds-otp-group |
Replaces the message when every box is empty. A code with some digits missing keeps the default message |
data-status |
.nds-otp-group |
The forms script writes it. error turns every box red. To set it yourself, call NDS.Form.setStatus() |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_otp.scss.
| Token | Preview | Value |
|---|---|---|
--border-neutral-primaryRead by background-color | var(--colors-neutral-300)Dark mode var(--colors-neutral-600) | |
--form-field-border-error | var(--border-error)Dark mode var(--border-error-light) |
JavaScript
| Method | Effect |
|---|---|
NDS.Otp.getValue(group) |
Returns the digits as one string, or '' when the group is not found. group is the .nds-otp-group, its id or a selector. An empty box adds nothing |
NDS.Otp.setValue(group, value) |
Fills the boxes from the first, and removes characters that are not digits. Returns true, or false when the group is not found. It fires no nds:otp event. The hidden input gets one input and change when its value changes |
NDS.Otp.clear(group) |
Empties the boxes, focuses the first box and fires nds:otp:clear. Returns true, or false when the group is not found. It does not remove an error: call NDS.Form.clearStatus(group) for that |
group.ndsOtp.getValue(), .setValue(value), .clear() |
The same, on a group that has started |
NDS.Otp.init() |
Starts every group on the page that has not started. It runs at page load, and a group added later starts on its own. The first group on a page that had none needs NDS.init.mount() on the new content. NDS.init.destroy(view) releases a group |
NDS.Otp.validate(group, options) |
Checks that every box holds a digit, shows the error when one is empty, and returns { valid, message, filled }. Pass { showMessage: false } to check without showing the error |
| Event | Fired on | Detail |
|---|---|---|
nds:otp:change |
.nds-otp-group, and it bubbles |
{ value, filled }. It fires on each typed, pasted or deleted digit. filled is true when every box holds a digit |
nds:otp:complete |
.nds-otp-group, and it bubbles |
{ value }. It fires when the last empty box gets a digit, in any order. Use it to submit the code |
nds:otp:clear |
.nds-otp-group, and it bubbles |
{ value }. It fires from NDS.Otp.clear() only |
The full API is in the banner of _js/nds-otp.js.