OTP Inputs
A one-time code in a single input: the phone fills it from a text message, a letter spacing makes it look like boxes, and the states, separator and Resend are covered.
Basic
One text input styled with a wide tracking-otp letter spacing, so the characters look like separate boxes without being separate fields. The phone offers the code from the text message because of autocomplete="one-time-code" and the numeric keypad because of inputmode="numeric". length (6 by default) sets maxlength and pattern, and a small Alpine handler removes anything that is not a digit.
Enter the 6 digit code from your authenticator app.
Enter your 4 digit PIN.
<div id="otp-six-field" class="max-w-xs group/field" x-data="{}">
<label
for="otp-six"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[:is(input,select,textarea):disabled]/field:opacity-50 group-has-[:is(input,select,textarea):required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>Verification code</label
>
<input
id="otp-six"
inputmode="numeric"
autocomplete="one-time-code"
maxlength="6"
pattern="[0-9]{6}"
placeholder="000000"
class="block w-full rounded-lg bg-white dark:bg-dominant-950 py-3 pr-3.5 pl-6 text-center font-mono text-2xl font-semibold tracking-otp text-dominant-900 dark:text-white inset-ring inset-ring-dominant-500 dark:inset-ring-dominant-400 placeholder:text-dominant-600 dark:placeholder:text-dominant-400 focus:inset-ring-primary-500 dark:focus:inset-ring-primary-400 focus:ring-2 focus:ring-primary-500/30 dark:focus:ring-primary-400/30 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:inset-ring-danger-600 dark:aria-invalid:inset-ring-danger-400 aria-invalid:focus:inset-ring-danger-600 dark:aria-invalid:focus:inset-ring-danger-400 aria-invalid:focus:ring-danger-600/30 dark:aria-invalid:focus:ring-danger-400/30"
type="text"
aria-describedby="otp-six-help"
x-on:input="$el.value = $el.value.replace(/\D/g, '')"
/>
<p id="otp-six-help" class="mt-1.5 text-sm text-dominant-600 dark:text-dominant-300">
Enter the 6 digit code from your authenticator app.
</p>
</div>
<div id="otp-four-field" class="max-w-xs group/field" x-data="{}">
<label
for="otp-four"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[:is(input,select,textarea):disabled]/field:opacity-50 group-has-[:is(input,select,textarea):required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>PIN</label
>
<input
id="otp-four"
inputmode="numeric"
autocomplete="one-time-code"
maxlength="4"
pattern="[0-9]{4}"
placeholder="0000"
class="block w-full rounded-lg bg-white dark:bg-dominant-950 py-3 pr-3.5 pl-6 text-center font-mono text-2xl font-semibold tracking-otp text-dominant-900 dark:text-white inset-ring inset-ring-dominant-500 dark:inset-ring-dominant-400 placeholder:text-dominant-600 dark:placeholder:text-dominant-400 focus:inset-ring-primary-500 dark:focus:inset-ring-primary-400 focus:ring-2 focus:ring-primary-500/30 dark:focus:ring-primary-400/30 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:inset-ring-danger-600 dark:aria-invalid:inset-ring-danger-400 aria-invalid:focus:inset-ring-danger-600 dark:aria-invalid:focus:inset-ring-danger-400 aria-invalid:focus:ring-danger-600/30 dark:aria-invalid:focus:ring-danger-400/30 max-w-56"
type="text"
aria-describedby="otp-four-help"
x-on:input="$el.value = $el.value.replace(/\D/g, '')"
/>
<p id="otp-four-help" class="mt-1.5 text-sm text-dominant-600 dark:text-dominant-300">
Enter your 4 digit PIN.
</p>
</div>
Alphanumeric and masked
kind="alphanumeric" uses inputmode="text", drops one-time-code, and keeps letters and digits in capitals. kind="masked" hides the PIN with type="password" and adds a Show PIN button (aria-pressed) so the person can check what they typed.
Eight letters or numbers.
The PIN is hidden while you type.
<div id="otp-alnum-field" class="max-w-xs group/field" x-data="{}">
<label
for="otp-alnum"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[:is(input,select,textarea):disabled]/field:opacity-50 group-has-[:is(input,select,textarea):required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>Recovery code</label
>
<input
id="otp-alnum"
inputmode="text"
autocomplete="off"
autocapitalize="characters"
spellcheck="false"
maxlength="8"
pattern="[A-Za-z0-9]{8}"
placeholder="XXXXXXXX"
class="block w-full rounded-lg bg-white dark:bg-dominant-950 py-3 pr-3.5 pl-6 text-center font-mono text-2xl font-semibold tracking-otp text-dominant-900 dark:text-white inset-ring inset-ring-dominant-500 dark:inset-ring-dominant-400 placeholder:text-dominant-600 dark:placeholder:text-dominant-400 focus:inset-ring-primary-500 dark:focus:inset-ring-primary-400 focus:ring-2 focus:ring-primary-500/30 dark:focus:ring-primary-400/30 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:inset-ring-danger-600 dark:aria-invalid:inset-ring-danger-400 aria-invalid:focus:inset-ring-danger-600 dark:aria-invalid:focus:inset-ring-danger-400 aria-invalid:focus:ring-danger-600/30 dark:aria-invalid:focus:ring-danger-400/30"
type="text"
aria-describedby="otp-alnum-help"
x-on:input="$el.value = $el.value.replace(/[^A-Za-z0-9]/g, '').toUpperCase()"
/>
<p id="otp-alnum-help" class="mt-1.5 text-sm text-dominant-600 dark:text-dominant-300">
Eight letters or numbers.
</p>
</div>
<div id="otp-masked-field" class="max-w-xs group/field" x-data="{ show: false }">
<label
for="otp-masked"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[:is(input,select,textarea):disabled]/field:opacity-50 group-has-[:is(input,select,textarea):required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>Security PIN</label
>
<input
id="otp-masked"
inputmode="numeric"
autocomplete="one-time-code"
maxlength="6"
pattern="[0-9]{6}"
placeholder="••••••"
class="block w-full rounded-lg bg-white dark:bg-dominant-950 py-3 pr-3.5 pl-6 text-center font-mono text-2xl font-semibold tracking-otp text-dominant-900 dark:text-white inset-ring inset-ring-dominant-500 dark:inset-ring-dominant-400 placeholder:text-dominant-600 dark:placeholder:text-dominant-400 focus:inset-ring-primary-500 dark:focus:inset-ring-primary-400 focus:ring-2 focus:ring-primary-500/30 dark:focus:ring-primary-400/30 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:inset-ring-danger-600 dark:aria-invalid:inset-ring-danger-400 aria-invalid:focus:inset-ring-danger-600 dark:aria-invalid:focus:inset-ring-danger-400 aria-invalid:focus:ring-danger-600/30 dark:aria-invalid:focus:ring-danger-400/30"
type="password"
x-bind:type="show ? 'text' : 'password'"
aria-describedby="otp-masked-help"
x-on:input="$el.value = $el.value.replace(/\D/g, '')"
/>
<button
id="otp-masked-toggle"
type="button"
aria-controls="otp-masked"
aria-pressed="false"
x-bind:aria-pressed="show"
x-on:click="show = !show"
x-text="show ? 'Hide PIN' : 'Show PIN'"
class="font-semibold rounded underline-offset-2 focus-visible:ring-2 focus-visible:outline-none cursor-pointer text-primary-700 dark:text-primary-300 focus-visible:ring-primary-700 dark:focus-visible:ring-primary-400 underline hover:text-primary-800 dark:hover:text-primary-200 mt-2 text-sm"
>
<span>Show PIN</span>
</button>
<p id="otp-masked-help" class="mt-1.5 text-sm text-dominant-600 dark:text-dominant-300">
The PIN is hidden while you type.
</p>
</div>
States
error sets aria-invalid="true" and points aria-describedby at the message: the border turns red and the message says what is wrong, so colour is not the only cue. success adds a green frame and a message, here with readonly. A disabled input is dimmed and skipped by the keyboard.
That code is not correct. Check it and try again.
Code accepted.
Request a new code to enter it here.
<div id="otp-error-field" class="max-w-xs group/field" x-data="{}">
<label
for="otp-error"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[:is(input,select,textarea):disabled]/field:opacity-50 group-has-[:is(input,select,textarea):required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>Verification code</label
>
<input
id="otp-error"
inputmode="numeric"
autocomplete="one-time-code"
maxlength="6"
pattern="[0-9]{6}"
placeholder="000000"
class="block w-full rounded-lg bg-white dark:bg-dominant-950 py-3 pr-3.5 pl-6 text-center font-mono text-2xl font-semibold tracking-otp text-dominant-900 dark:text-white inset-ring inset-ring-dominant-500 dark:inset-ring-dominant-400 placeholder:text-dominant-600 dark:placeholder:text-dominant-400 focus:inset-ring-primary-500 dark:focus:inset-ring-primary-400 focus:ring-2 focus:ring-primary-500/30 dark:focus:ring-primary-400/30 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:inset-ring-danger-600 dark:aria-invalid:inset-ring-danger-400 aria-invalid:focus:inset-ring-danger-600 dark:aria-invalid:focus:inset-ring-danger-400 aria-invalid:focus:ring-danger-600/30 dark:aria-invalid:focus:ring-danger-400/30"
value="123456"
type="text"
aria-describedby="otp-error-error"
aria-invalid="true"
x-on:input="$el.value = $el.value.replace(/\D/g, '')"
/>
<p
id="otp-error-error"
class="flex items-start gap-1.5 mt-1.5 text-sm font-medium text-danger-700 dark:text-danger-300"
>
<i aria-hidden="true" class="mt-0.5 fa-solid fa-circle-exclamation"></i
><span>That code is not correct. Check it and try again.</span>
</p>
</div>
<div id="otp-success-field" class="max-w-xs group/field" x-data="{}">
<label
for="otp-success"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[:is(input,select,textarea):disabled]/field:opacity-50 group-has-[:is(input,select,textarea):required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>Verification code</label
>
<input
id="otp-success"
inputmode="numeric"
autocomplete="one-time-code"
maxlength="6"
pattern="[0-9]{6}"
placeholder="000000"
class="block w-full rounded-lg bg-white dark:bg-dominant-950 py-3 pr-3.5 pl-6 text-center font-mono text-2xl font-semibold tracking-otp text-dominant-900 dark:text-white inset-ring inset-ring-dominant-500 dark:inset-ring-dominant-400 placeholder:text-dominant-600 dark:placeholder:text-dominant-400 focus:inset-ring-primary-500 dark:focus:inset-ring-primary-400 focus:ring-2 focus:ring-primary-500/30 dark:focus:ring-primary-400/30 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:inset-ring-danger-600 dark:aria-invalid:inset-ring-danger-400 aria-invalid:focus:inset-ring-danger-600 dark:aria-invalid:focus:inset-ring-danger-400 aria-invalid:focus:ring-danger-600/30 dark:aria-invalid:focus:ring-danger-400/30 inset-ring-success-700 dark:inset-ring-success-400"
value="482915"
readonly
type="text"
aria-describedby="otp-success-success"
x-on:input="$el.value = $el.value.replace(/\D/g, '')"
/>
<p
id="otp-success-success"
class="flex items-start gap-1.5 mt-1.5 text-sm font-medium text-success-700 dark:text-success-300"
>
<i aria-hidden="true" class="mt-0.5 fa-solid fa-circle-check"> </i>
<span>Code accepted.</span>
</p>
</div>
<div id="otp-disabled-field" class="max-w-xs group/field" x-data="{}">
<label
for="otp-disabled"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[:is(input,select,textarea):disabled]/field:opacity-50 group-has-[:is(input,select,textarea):required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>Verification code</label
>
<input
id="otp-disabled"
inputmode="numeric"
autocomplete="one-time-code"
maxlength="6"
pattern="[0-9]{6}"
placeholder="000000"
class="block w-full rounded-lg bg-white dark:bg-dominant-950 py-3 pr-3.5 pl-6 text-center font-mono text-2xl font-semibold tracking-otp text-dominant-900 dark:text-white inset-ring inset-ring-dominant-500 dark:inset-ring-dominant-400 placeholder:text-dominant-600 dark:placeholder:text-dominant-400 focus:inset-ring-primary-500 dark:focus:inset-ring-primary-400 focus:ring-2 focus:ring-primary-500/30 dark:focus:ring-primary-400/30 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:inset-ring-danger-600 dark:aria-invalid:inset-ring-danger-400 aria-invalid:focus:inset-ring-danger-600 dark:aria-invalid:focus:inset-ring-danger-400 aria-invalid:focus:ring-danger-600/30 dark:aria-invalid:focus:ring-danger-400/30"
disabled
type="text"
aria-describedby="otp-disabled-help"
x-on:input="$el.value = $el.value.replace(/\D/g, '')"
/>
<p id="otp-disabled-help" class="mt-1.5 text-sm text-dominant-600 dark:text-dominant-300">
Request a new code to enter it here.
</p>
</div>
Separator and Resend
separator adds a hyphen (3-3) while the person types, so it stays one input; the value then holds the hyphen, so remove it before you check the code. The page writes the Resend link below the component; it is disabled with a countdown; when it ends, a role="status" message tells a screen reader the code was sent. The countdown is not announced every second.
The hyphen is added for you. Remove it before you check the code.
Did not get a code?
<div id="otp-separator-field" class="max-w-xs group/field" x-data="{}">
<label
for="otp-separator"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[:is(input,select,textarea):disabled]/field:opacity-50 group-has-[:is(input,select,textarea):required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>Verification code</label
>
<input
id="otp-separator"
inputmode="numeric"
autocomplete="one-time-code"
maxlength="7"
pattern="[0-9]{3}-[0-9]{3}"
placeholder="000-000"
class="block w-full rounded-lg bg-white dark:bg-dominant-950 py-3 pr-3.5 pl-6 text-center font-mono text-2xl font-semibold tracking-otp text-dominant-900 dark:text-white inset-ring inset-ring-dominant-500 dark:inset-ring-dominant-400 placeholder:text-dominant-600 dark:placeholder:text-dominant-400 focus:inset-ring-primary-500 dark:focus:inset-ring-primary-400 focus:ring-2 focus:ring-primary-500/30 dark:focus:ring-primary-400/30 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:inset-ring-danger-600 dark:aria-invalid:inset-ring-danger-400 aria-invalid:focus:inset-ring-danger-600 dark:aria-invalid:focus:inset-ring-danger-400 aria-invalid:focus:ring-danger-600/30 dark:aria-invalid:focus:ring-danger-400/30"
type="text"
aria-describedby="otp-separator-help"
x-on:input="const d = $el.value.replace(/\D/g, '').slice(0, 6); $el.value = d.length > 3 ? d.slice(0, 3) + '-' + d.slice(3) : d"
/>
<p id="otp-separator-help" class="mt-1.5 text-sm text-dominant-600 dark:text-dominant-300">
The hyphen is added for you. Remove it before you check the code.
</p>
</div>
<div
x-data="{ left: 30, sent: false, get clock() { return Math.floor(this.left / 60) + ':' + String(this.left % 60).padStart(2, '0'); } }"
x-init="setInterval(() => { if (left > 0) left -= 1; }, 1000)"
>
<div id="otp-resend-field" class="max-w-xs group/field" x-data="{}">
<label
for="otp-resend"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[:is(input,select,textarea):disabled]/field:opacity-50 group-has-[:is(input,select,textarea):required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>Verification code</label
>
<input
id="otp-resend"
inputmode="numeric"
autocomplete="one-time-code"
maxlength="6"
pattern="[0-9]{6}"
placeholder="000000"
class="block w-full rounded-lg bg-white dark:bg-dominant-950 py-3 pr-3.5 pl-6 text-center font-mono text-2xl font-semibold tracking-otp text-dominant-900 dark:text-white inset-ring inset-ring-dominant-500 dark:inset-ring-dominant-400 placeholder:text-dominant-600 dark:placeholder:text-dominant-400 focus:inset-ring-primary-500 dark:focus:inset-ring-primary-400 focus:ring-2 focus:ring-primary-500/30 dark:focus:ring-primary-400/30 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:inset-ring-danger-600 dark:aria-invalid:inset-ring-danger-400 aria-invalid:focus:inset-ring-danger-600 dark:aria-invalid:focus:inset-ring-danger-400 aria-invalid:focus:ring-danger-600/30 dark:aria-invalid:focus:ring-danger-400/30"
type="text"
x-on:input="$el.value = $el.value.replace(/\D/g, '')"
/>
</div>
<p class="mt-3 text-sm text-dominant-700 dark:text-dominant-200">
Did not get a code?
<button
id="otp-resend-link"
type="button"
x-bind:disabled="left > 0"
x-on:click="sent = true; left = 30"
x-text="left > 0 ? 'Resend code in ' + clock : 'Resend code'"
class="font-semibold rounded underline-offset-2 focus-visible:ring-2 focus-visible:outline-none cursor-pointer text-primary-700 dark:text-primary-300 focus-visible:ring-primary-700 dark:focus-visible:ring-primary-400 underline hover:text-primary-800 dark:hover:text-primary-200 disabled:cursor-not-allowed disabled:text-dominant-700 dark:disabled:text-dominant-300 disabled:no-underline"
>
<span>Resend code</span>
</button>
</p>
<p
role="status"
x-show="sent"
style="display: none"
class="mt-1 text-xs font-medium text-success-700 dark:text-success-300"
>
<i aria-hidden="true" class="fa-solid fa-check"> </i> A new code was sent.
</p>
</div>
Options reference
The input is plain HTML with Tailwind classes; Alpine only filters the characters and runs the Resend countdown.
| Part | Value | Description |
|---|---|---|
type="text"
|
with inputmode
|
Do not use type="number": it removes leading zeros, adds spinners and accepts e.
|
inputmode
|
numeric or text
|
Which keyboard the phone shows. |
autocomplete
|
one-time-code
|
Lets the phone fill the code from a text message. Use off for recovery codes.
|
maxlength, pattern
|
length and format |
For example 6 and [0-9]{6}.
|
tracking-otp
|
theme value 0.6em
|
Defined in the @theme of the page: --tracking-otp: 0.6em. The left padding (pl-6) is a little larger than the right one to put the text back in the middle.
|
@input
|
Alpine, optional |
Removes characters that are not allowed. Without Alpine the pattern still blocks submit.
|
aria-invalid
|
true
|
Turns the border red; add aria-describedby for the message.
|
readonly, disabled
|
attributes | Success (with green border) and disabled states. |