Individually styled digit fields for one-time password, PIN, and verification code entry, with auto-advance, paste handling, and keyboard navigation.
Introduction
An OTP (one-time password) input groups single-character fields into one control for verification codes, PINs, and two-factor authentication.
The component builds on form inputs. Its plugin moves the focus to the next field after each digit and back to the previous one on Backspace, distributes a pasted code across the fields, and moves between fields with the arrow keys. The first field receives autocomplete="one-time-code", which lets browsers that support it fill in a code from an SMS or an email.
Basic structure
Wrap the inputs in a container with .form-otp and add data-cx-otp to enable the plugin. Give each input the .form-input class; the plugin limits every field to a single digit.
<div class="form-otp" data-cx-otp role="group" aria-label="Verification code">
<input type="text" class="form-input" aria-label="Digit 1">
<input type="text" class="form-input" aria-label="Digit 2">
<input type="text" class="form-input" aria-label="Digit 3">
<input type="text" class="form-input" aria-label="Digit 4">
<input type="text" class="form-input" aria-label="Digit 5">
<input type="text" class="form-input" aria-label="Digit 6">
</div>Connected inputs
Add .input-group to the container to join the fields into a single bordered control with the input group styles.
<div class="form-otp input-group" data-cx-otp role="group" aria-label="Verification code">
<input type="text" class="form-input" aria-label="Digit 1">
<input type="text" class="form-input" aria-label="Digit 2">
<input type="text" class="form-input" aria-label="Digit 3">
<input type="text" class="form-input" aria-label="Digit 4">
<input type="text" class="form-input" aria-label="Digit 5">
<input type="text" class="form-input" aria-label="Digit 6">
</div>Four-digit PIN
The plugin works with the inputs it finds in the container, whatever their number. Use fewer inputs for shorter codes such as a four-digit PIN.
<div class="form-otp input-group" data-cx-otp role="group" aria-label="Verification code">
<input type="text" class="form-input" aria-label="Digit 1">
<input type="text" class="form-input" aria-label="Digit 2">
<input type="text" class="form-input" aria-label="Digit 3">
<input type="text" class="form-input" aria-label="Digit 4">
</div>With separator
Add a .form-otp-separator element between inputs to group a code as in "123-456". The separator is visual only: the plugin ignores the elements that are not inputs.
<div class="form-otp" data-cx-otp role="group" aria-label="Verification code">
<input type="text" class="form-input" aria-label="Digit 1">
<input type="text" class="form-input" aria-label="Digit 2">
<input type="text" class="form-input" aria-label="Digit 3">
<span class="form-otp-separator">–</span>
<input type="text" class="form-input" aria-label="Digit 4">
<input type="text" class="form-input" aria-label="Digit 5">
<input type="text" class="form-input" aria-label="Digit 6">
</div>A separator also works with connected inputs: wrap each group of fields in a nested .input-group.
<div class="form-otp" data-cx-otp role="group" aria-label="Verification code">
<div class="input-group">
<input type="text" class="form-input" aria-label="Digit 1">
<input type="text" class="form-input" aria-label="Digit 2">
<input type="text" class="form-input" aria-label="Digit 3">
</div>
<span class="form-otp-separator">–</span>
<div class="input-group">
<input type="text" class="form-input" aria-label="Digit 4">
<input type="text" class="form-input" aria-label="Digit 5">
<input type="text" class="form-input" aria-label="Digit 6">
</div>
</div>Disabled
Add the disabled attribute to each input to prevent interaction.
<div class="form-otp input-group" data-cx-otp role="group" aria-label="Verification code">
<input type="text" class="form-input" aria-label="Digit 1" disabled>
<input type="text" class="form-input" aria-label="Digit 2" disabled>
<input type="text" class="form-input" aria-label="Digit 3" disabled>
<input type="text" class="form-input" aria-label="Digit 4" disabled>
<input type="text" class="form-input" aria-label="Digit 5" disabled>
<input type="text" class="form-input" aria-label="Digit 6" disabled>
</div>Validation
Add .is-valid or .is-invalid to the container to show a validation state on all of its fields.
<div class="form-otp input-group is-valid mb-md" data-cx-otp role="group" aria-label="Verification code">
<input type="text" class="form-input" value="1" aria-label="Digit 1">
<input type="text" class="form-input" value="2" aria-label="Digit 2">
<input type="text" class="form-input" value="3" aria-label="Digit 3">
<input type="text" class="form-input" value="4" aria-label="Digit 4">
<input type="text" class="form-input" value="5" aria-label="Digit 5">
<input type="text" class="form-input" value="6" aria-label="Digit 6">
</div>
<div class="form-otp input-group is-invalid" data-cx-otp role="group" aria-label="Verification code">
<input type="text" class="form-input" value="1" aria-label="Digit 1">
<input type="text" class="form-input" value="2" aria-label="Digit 2">
<input type="text" class="form-input" value="3" aria-label="Digit 3">
<input type="text" class="form-input" aria-label="Digit 4">
<input type="text" class="form-input" aria-label="Digit 5">
<input type="text" class="form-input" aria-label="Digit 6">
</div>With form labels
Inside a form field, a form label names the group and a help text describes the expected code.
<div class="form-field">
<label class="form-label">Verification code</label>
<div class="form-otp" data-cx-otp role="group" aria-label="Verification code" aria-describedby="otpHelp">
<input type="text" class="form-input" aria-label="Digit 1">
<input type="text" class="form-input" aria-label="Digit 2">
<input type="text" class="form-input" aria-label="Digit 3">
<input type="text" class="form-input" aria-label="Digit 4">
<input type="text" class="form-input" aria-label="Digit 5">
<input type="text" class="form-input" aria-label="Digit 6">
</div>
<div id="otpHelp" class="form-help">Enter the 6-digit code sent to your phone.</div>
</div>Accessibility
The fields of an OTP input have no visible labels of their own, so the markup has to name each field and the group. The plugin sets no ARIA attribute.
Individual input labels
Each digit input needs an aria-label describing its position — for example aria-label="Digit 1". Without it, screen readers have no way to distinguish between the fields.
Container labeling
Label the OTP group as a whole with a visible <label> linked through aria-labelledby on the container, so screen readers announce the purpose of the group before the individual inputs:
<label class="form-label" id="otpLabel">Verification code</label>
<div class="form-otp" data-cx-otp role="group" aria-labelledby="otpLabel">
...
</div>When no visible label is appropriate, use aria-label on the container instead. Both attributes need role="group" on the container to take effect.
Help text
Associate instructional text with the group through aria-describedby on the container:
<div class="form-otp" data-cx-otp role="group" aria-describedby="otpHelp">
...
</div>
<div id="otpHelp" class="form-help">Enter the 6-digit code sent to your phone.</div>Keyboard and mobile
The plugin sets inputmode="numeric" and pattern="\d*" on each input, which brings up the numeric keyboard on mobile devices without changing the input type, and it removes any character that is not a digit.
| Key | Action |
|---|---|
| A digit | Fill the field and move to the next one. |
| ← / → | Move to the previous or next field. |
| Backspace | In an empty field, clear the previous field and move to it. |
| Delete | Clear the field and shift the digits after it one field back. |
JavaScript API
The OtpInput plugin handles the focus, the paste and the keys of the fields, and fires an event when the code is complete. Chassis JS ships as an ES module — import the OtpInput class:
import { OtpInput } from '@chassis-ui/css'Triggers
A container with data-cx-otp is initialized when the page loads.
| Attribute | Description |
|---|---|
data-cx-otp | Initializes the OTP input on the container wrapping the digit fields. |
data-cx-mask | When true, uses password fields to mask entered digits. |
<div class="form-otp" data-cx-otp role="group" aria-label="Verification code">
<input type="text" class="form-input" aria-label="Digit 1">
<input type="text" class="form-input" aria-label="Digit 2">
<input type="text" class="form-input" aria-label="Digit 3">
<input type="text" class="form-input" aria-label="Digit 4">
</div>Initialization
Create an instance with the OtpInput class for a container added after the page has loaded, or to call its methods:
const otpElement = document.querySelector('.form-otp')
const otpInput = new OtpInput(otpElement)Methods
The plugin exposes instance methods to read, set and clear the code:
| Method | Description |
|---|---|
getValue() | Returns the complete OTP value as a string. |
setValue(value) | Sets the OTP value, distributing characters across inputs. |
clear() | Clears all inputs and focuses the first one. |
focus() | Focuses the first empty input, or the last input if all are filled. |
dispose() | Destroys the component instance. |
getInstance(element) | Static — returns the instance bound to a container, or null. |
getOrCreateInstance(element) | Static — returns the instance bound to a container, or creates one. |
const otpElement = document.querySelector('.form-otp')
const otpInput = OtpInput.getOrCreateInstance(otpElement)
// Get the current value
console.log(otpInput.getValue()) // "123456"
// Set a value programmatically
otpInput.setValue('654321')
// Clear all inputs
otpInput.clear()Options
Options are set via data-cx-* attributes or passed to the constructor as an object. Attribute names use the kebab-case form of the option name — data-cx-custom-class, not data-cx-customClass. Attribute values are parsed to their native types:"true" → true, "0" → 0, and valid JSON strings to objects.
Use data-cx-config to pass multiple options as a JSON string:data-cx-config='{"delay":200}'. Individual data-cx-* attributes take precedence over data-cx-config. Individual attributes accept JSON values too, such as data-cx-delay='{"show":100,"hide":200}'.
When initializing components, Chassis merges configurations from multiple sources in this priority order: default settings, data-cx-config values, individual data-cx-*attributes, and finally any JavaScript object options. Values defined later in this sequence override earlier ones.
| Name | Type | Default | Description |
|---|---|---|---|
mask | boolean | false | If true, the inputs use type="password" to hide the entered values. |
Events
Both events fire on the container.
| Event | Description |
|---|---|
complete.cx.otp-input | Fired when all inputs are filled: after typing, a paste, or setValue(). The event's value property contains the complete code. |
input.cx.otp-input | Fired on each input change made in the fields: typing, Backspace, Delete and paste. Includes value (current combined value) and index (changed input index). Not fired by setValue() or clear(). |
const otpElement = document.querySelector('.form-otp')
otpElement.addEventListener('complete.cx.otp-input', event => {
console.log('OTP complete:', event.value)
// Submit the form or validate the code
})
otpElement.addEventListener('input.cx.otp-input', event => {
console.log('Current value:', event.value, 'Changed index:', event.index)
})CSS
The OTP input component can be customized through Sass variables at compile time.
Custom properties
The OTP input component exposes CSS custom properties to control its appearance at runtime.
--otp-gap: #{$form-otp-gap};
--otp-input-w: #{$form-otp-input-w};
--otp-input-h: #{$form-otp-input-h};
--otp-font-size: #{$form-otp-font-size};
--otp-font-weight: #{$form-otp-font-weight};
--otp-separator-color: #{$form-otp-separator-color};Sass variables
The OTP input component uses Sass variables in scss/config/_defaults.scss to define its defaults.
$form-otp-input-w: var(--size-md);
$form-otp-input-h: var(--size-lg);
$form-otp-gap: var(--space-xs);
$form-otp-font-size: var(--font-size-ui-xl);
$form-otp-font-weight: var(--font-weight-ui-strong);
$form-otp-separator-color: var(--fg-subtle);