Skip to main content
Login

Sign in to access your account

OTP Input - National Design System

A row of one-digit boxes for a one-time code, where focus moves to the next box as the user types

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:

  • the user picks a new password: Password
  • the value is a number with no fixed length: a number field in Forms

Markup

Preview
Verification code
<fieldset class="nds-form-group nds-otp-group" data-required> <legend><span class="nds-label">Verification code</span></legend> <div class="nds-otp"> <div class="nds-form-container nds-otp-container"> <div class="nds-form-control"> <input type="text" id="otp-digit-1" inputmode="numeric" maxlength="1" pattern="[0-9]" autocomplete="one-time-code" aria-label="Digit 1 of 4"> </div> </div> <div class="nds-form-container nds-otp-container"> <div class="nds-form-control"> <input type="text" id="otp-digit-2" inputmode="numeric" maxlength="1" pattern="[0-9]" aria-label="Digit 2 of 4"> </div> </div> <div class="nds-form-container nds-otp-container"> <div class="nds-form-control"> <input type="text" id="otp-digit-3" inputmode="numeric" maxlength="1" pattern="[0-9]" aria-label="Digit 3 of 4"> </div> </div> <div class="nds-form-container nds-otp-container"> <div class="nds-form-control"> <input type="text" id="otp-digit-4" inputmode="numeric" maxlength="1" pattern="[0-9]" aria-label="Digit 4 of 4"> </div> </div> </div> <input type="hidden" class="nds-otp-value" name="otp"> </fieldset>

Built-in Features

  • Auto-initialization

    Every nds-otp-group on the page starts on its own, and so does a group added later. The first group on a page that had none needs NDS.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-value input holds the whole code after each change, so the form sends it as one field.

  • Submit Check

    In a form, a group with data-required blocks 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 autofocus gets 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-value input, not to the boxes. A named box sends its digit as one more field.
  • Keep the aria-label the 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-required on 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 autofocus on 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.

TokenPreviewValue
--border-neutral-primary
Read 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
var group = document.querySelector('#verify-form .nds-otp-group'); // Submit when the last digit is in group.addEventListener('nds:otp:complete', function () { document.querySelector('#verify-form').requestSubmit(); }); // Show the server's answer on the boxes NDS.Form.setStatus(group, 'error', { message: 'This code is not correct' }); // Empty the boxes after you send a new code NDS.Otp.clear(group); NDS.Form.clearStatus(group);

The full API is in the banner of _js/nds-otp.js.

Last Modified Date: 05/11/2026 - 03:30 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks