OTP input
One-time code entry split into single-digit cells.
Example
<div class="stack"> <focsd-otp-field label="Verification code" length="6" required error-message="Enter the 6-digit code from your text message"></focsd-otp-field> <focsd-otp-field label="Expired code" length="6" value="482913" error="This code expired. Request a new one."></focsd-otp-field> <focsd-otp-field label="Backup code" length="6" value="123456" warning-pattern="(?!(\d)\1{5})(?!123456)\d{6}" warning-message="This looks easy to guess. Check it before you continue."></focsd-otp-field> <focsd-otp-field label="Confirmed code" length="4" value="5821" success="Code confirmed."></focsd-otp-field></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-otp-field.js';</script>
<focsd-otp-field label="Verification code" length="6" required error-message="Enter the 6-digit code from your text message"></focsd-otp-field>import { OtpField } from '@focsd-dev/react';
export function Example() { return <OtpField onChange={(e) => console.log(e.target)} label="Verification code" length="6" required error-message="Enter the 6-digit code from your text message"></OtpField>;}<script setup>import '@focsd-dev/components/focsd-otp-field.js';</script>
<template> <focsd-otp-field @change="onChange" label="Verification code" length="6" required error-message="Enter the 6-digit code from your text message"></focsd-otp-field></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-otp-field.js';
// template<focsd-otp-field (change)="onChange($event)" label="Verification code" length="6" required error-message="Enter the 6-digit code from your text message"></focsd-otp-field><script> import '@focsd-dev/components/focsd-otp-field.js';</script>
<focsd-otp-field onchange={(e) => console.log(e.target)} label="Verification code" length="6" required error-message="Enter the 6-digit code from your text message"></focsd-otp-field>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
name | name | string | — |
disabled | disabled | boolean | false |
required | required | boolean | false |
readonly | readOnly | boolean | false |
aria-label | hostAriaLabel | string | null | null |
aria-description | hostAriaDescription | string | null | null |
aria-labelledby | hostAriaLabelledBy | string | null | null |
aria-describedby | hostAriaDescribedBy | string | null | null |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
hint | hint | string | — |
error | error | — | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
error-message | errorMessage | string | undefined | — |
warning-message | warningMessage | string | undefined | — |
warning-pattern | warningPattern | string | undefined | — |
label | label | string | — |
length | length | number | 6 |
value | value | string | — |
autocomplete | autocomplete | AutoFill | one-time-code |
Methods
| Method | Description |
|---|---|
describedByIds() | IDs to add to the inner control's aria-describedby: the hint, then the shown message. |
checkValidity() | True when the value is valid; otherwise fires invalid and returns false, like native. |
reportValidity() | Like checkValidity(), and also shows the browser's validation message when invalid. |
setCustomValidity(message) | Set (non-empty) or clear (empty string) a custom validation error, like native controls. |
setValidityState(flags, message, anchor) | For subclasses: set the validity flags and message. anchor is the element the browser points its message at. |
validate() | Show (or clear) the error from the control's own checks: required, type, pattern, min,
max and the like. Uses error-message, else the browser's localized message. An error the
author set (a server error, say) is left as it is. |
clearValidation() | Clear the error and warning that validate() and warning-pattern showed (a form reset does). |
emitValue(commit) | For subclasses: fire the value events, as a native control does on a user edit: input
(bubbles, composed), then with commit also change (bubbles, stays in its tree). |
idrefText(ids) | For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces. |
Events
| Event | Description |
|---|---|
input | Fired on every user edit of the value. |
change | — |
CSS parts
Style the inside of the component with ::part(), for example focsd-otp-field::part(layout).
| Part | Description |
|---|---|
layout | The wrapper that places the label, the body and the messages. |
body | The group of cells, with the hint and message below it. |
cell | The cell element. |
control | The control element. |
group | The group element. |
label | The label element. |
hint | The author's hint. |
message | The shown error, warning or success message. |
message-icon | The icon in the message. |
announcer | The visually hidden live region. |
Accessibility
autocompletedefaults toone-time-code, so browsers can fill codes from SMS.- A labeled
role="group"; each cell is named "Digit n of 6". - Pasting a code fills the cells from the first one.
- The cells take the field's state:
error,warningandsuccess(orerror-messageandwarning-patternwithwarning-message, checked for you) color every cell's border and show the message under the group. - Form-associated: participates in
FormData, validation, reset and fieldsetdisabledlike a native control.
Keyboard
| Key | Action |
|---|---|
| Digits | Fill the cell and move to the next |
| Backspace (empty cell) | Clear the previous cell and move to it |
| Arrow Left / Right | Previous / next cell |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.