Actions and inputs
One-time code
<kui-otp> draws one box per digit over a single real input. That input stays the one control, so paste, the phone’s suggested code from an SMS and screen readers all work.
Examples
A six-digit code from an SMS
autocomplete="one-time-code" lets the phone offer the code from the message. The element keeps only digits and fires kui-complete when the last one is entered. Try pasting “48 27 19”.
We sent it to +381 64 ••• 27 19. It is valid for 10 minutes.
<div class="kui-field">
<label class="kui-label" for="otp-sms">Code from the SMS</label>
<kui-otp class="kui-otp">
<input class="kui-input" id="otp-sms" name="code" inputmode="numeric" autocomplete="one-time-code" maxlength="6" aria-describedby="otp-sms-hint">
</kui-otp>
<p class="kui-hint" id="otp-sms-hint">We sent it to +381 64 ••• 27 19. It is valid for 10 minutes.</p>
</div>
Letters and digits
With inputmode="text" the code may hold letters too. maxlength sets the number of boxes.
<div class="kui-field">
<label class="kui-label" for="otp-pair">Pairing code shown on the TV</label>
<kui-otp class="kui-otp">
<input class="kui-input" id="otp-pair" name="pair" inputmode="text" autocapitalize="characters" autocomplete="off" maxlength="4">
</kui-otp>
</div>
States
Every state
The box where the next character goes has the focus ring and a blinking caret (still when reduced motion is asked for). Filled boxes show the digit. Invalid outlines every box in the danger colour, with the message under them. Without JavaScript it is one plain wide field with spaced digits.
This code is not right. Check the SMS or ask for a new one.
<div class="kui-field">
<label class="kui-label" for="otp-s1">Wrong code</label>
<kui-otp class="kui-otp">
<input class="kui-input" id="otp-s1" inputmode="numeric" autocomplete="one-time-code" maxlength="6" value="482710" aria-invalid="true" aria-describedby="otp-s1-e">
</kui-otp>
<p class="kui-error-text" id="otp-s1-e"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#circle-alert"></use></svg><span>This code is not right. Check the SMS or ask for a new one.</span></p>
</div>
Classes and attributes
kui-otp.kui-otp- The element around one input.
input maxlength- The number of characters and of boxes (6 when not set).
input inputmode="numeric" or "text"- Digits only, or letters and digits.
autocomplete="one-time-code"- Lets the phone fill the code from the SMS.
.kui-otp__cells, .kui-otp__cell- The boxes the element draws, hidden from screen readers.
event kui-complete- Fired with detail.value when every box is filled.
Keyboard
- Digits or letters
- Fill the next box.
- Backspace
- Clears the last box.
- Ctrl + V or Cmd + V
- Pastes the whole code; spaces and dashes are removed.
Accessibility
- There is one input with one label, so a screen reader reads one field and its whole value, not six fields.
- The boxes are drawn with aria-hidden="true".
- Do not send the code the moment it is complete without a way to correct it; let kui-complete start the check and keep a Confirm button.
- Say where the code was sent and how long it is valid, in the hint.
Do and do not
Do
- Keep a link to send a new code next to the field.
- Accept the code pasted with spaces or dashes.
- Say how many attempts are left after a wrong code.
Do not
- Do not split the code into one input per digit.
- Do not clear the field after a wrong code without saying why.
- Do not set a time limit shorter than a few minutes.