Two factor and sign in
Verification code
One cell per character on top of the CSS only .otp-field: auto advance, Backspace goes back, arrows, Home and End, paste or SMS autofill of the whole code from any cell (spaces and dashes are stripped), error shake and success states, masked mode and a monsoon:otp-complete event. The field stays in the form with the full value. Needs monsoon.js.
Needs monsoon.js. The styles work on their own, and the behavior comes from the optional interactive layer: add <script src="https://cdn.jsdelivr.net/npm/monsooncss/dist/monsoon.js" defer></script> or import 'monsooncss/js'. See the interactive layer.
Example
<div class="grid gap-5">
<div class="otp" data-m-otp data-m-otp-separator="3">
<input class="otp-field" name="code" maxlength="6" inputmode="numeric" autocomplete="one-time-code" aria-label="Verification code">
</div>
<div class="otp otp-lg otp-primary" data-m-otp data-m-otp-length="8" data-m-otp-type="alphanumeric" data-m-otp-separator="4">
<input class="otp-field" name="recovery" maxlength="8" inputmode="text" autocomplete="one-time-code" aria-label="Recovery code">
</div>
<div class="flex flex-wrap items-center gap-4">
<div class="otp otp-sm otp-masked" data-m-otp data-m-otp-length="4">
<input class="otp-field" name="pin" maxlength="4" inputmode="numeric" autocomplete="one-time-code" aria-label="PIN">
</div>
<div class="otp otp-sm" data-m-otp data-m-otp-length="4" data-m-otp-disabled>
<input class="otp-field" name="locked" maxlength="4" inputmode="numeric" value="2580" aria-label="Locked code" disabled>
</div>
</div>
</div>JavaScript API
Attribute
data-m-otp
Options
| Option | Description |
|---|---|
data-m-otp-length="6" (4 to 8, or the field maxlength) | |
data-m-otp-separator="3" (a dash after cell 3, or "2,4") | |
data-m-otp-type="alphanumeric" (letters and digits, shown upper case) | |
data-m-otp-pattern="[0-9a-f]" (any one character class) | |
data-m-otp-masked (dots instead of characters) | |
data-m-otp-autosubmit (submits the form when complete) | |
data-m-otp-webotp (reads the SMS with the WebOTP API where supported) | |
data-m-otp-name="code" (field name when the markup has no field) |
Methods
| Method | Description |
|---|---|
Monsoon.otp(el).value | |
Monsoon.otp(el).value = "482913" | |
Monsoon.otp(el).complete | |
Monsoon.otp(el).error(clear) | |
Monsoon.otp(el).success() | |
Monsoon.otp(el).reset() | |
Monsoon.otp(el).clear() | |
Monsoon.otp(el).focus() | |
Monsoon.otp(el).disabled = true |
Events
| Event | Description |
|---|---|
monsoon:otp-change { value, complete } | |
monsoon:otp-complete { code } |
Classes
5 classes. Click a name to copy it. Utilities on the same element always win over component defaults, so any of them can be adjusted with plain utility classes.
| Class | Type | What it does |
|---|---|---|
| Base | Base class of the verification code. On a <div> in the example. Sets flex display, alignment, gap, width, absolute position and offsets, and 7 custom properties.CSS |
|
| Part | Part of the verification code. On an <input> in the example. Sets block display, width, height, padding, text size and text style, and 13 custom properties.CSS |
|
| Modifier | Modifier, combined with the otp class. Not used in the example above. Sets block display, width, height, padding, text size and text style, and 10 custom properties.CSS |
|
| Modifier | Modifier, combined with the otp class. Not used in the example above. Sets alignment, width, height, corner radius and background.CSS |
|
| Modifier | Modifier, combined with the otp class. On a <div> in the example. A hook for state rules such as .otp-masked > .otp-input.CSS |
Usage
The styles ship inside the same stylesheet as the utilities, and the behavior comes from the optional monsoon.js script, which enhances any element with a matching data-m attribute, including markup added later.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/monsooncss">
<script src="https://cdn.jsdelivr.net/npm/monsooncss/dist/monsoon.js" defer></script>
<!-- Verification code: start from the base class, then add modifiers -->
<div class="grid gap-5">
<div class="otp" data-m-otp data-m-otp-separator="3">
<input class="otp-field" name="code" maxlength="6" inputmode="numeric" autocomplete="one-time-code" aria-label="Verification code">
</div>
<div class="otp otp-lg otp-primary" data-m-otp data-m-otp-length="8" data-m-otp-type="alphanumeric" data-m-otp-separator="4">
<input class="otp-field" name="recovery" maxlength="8" inputmode="text" autocomplete="one-time-code" aria-label="Recovery code">
</div>
<div class="flex flex-wrap items-center gap-4">
<div class="otp otp-sm otp-masked" data-m-otp data-m-otp-length="4">
<input class="otp-field" name="pin" maxlength="4" inputmode="numeric" autocomplete="one-time-code" aria-label="PIN">
</div>
<div class="otp otp-sm" data-m-otp data-m-otp-length="4" data-m-otp-disabled>
<input class="otp-field" name="locked" maxlength="4" inputmode="numeric" value="2580" aria-label="Locked code" disabled>
</div>
<!-- the full example is in the Code tab above -->Theme it with --seed on any ancestor, for example style="--seed: #0d9488", and force a scheme with class="dark" or class="light" on a wrapper.
Accessibility
- Give every control a visible
labellinked withfor, or anaria-labelwhen the purpose is obvious from context. Link help and error text witharia-describedby; invalid styles use:user-invalid, so they appear only after the user interacts. - Every interactive part shows a visible focus ring with
:focus-visible, and tone colors pair with-foregroundtokens that keep text at 4.5:1 or better in light and dark mode.