OTP Input Examples
The examples below assume that you have imported the component and set up the theme.
Basic
Add a field for a one-time code, like a sign-in code sent by SMS or email. name submits the code with the form, and a <label for> names the field.
--empty--complete
<form class="otp-input-basic"> <label for="otp-input-code">Verification code</label> <sv-otp-input id="otp-input-code" name="code"></sv-otp-input></form>
<script type="application/x-typescript" module> const form = document.querySelector<HTMLFormElement>(".otp-input-basic")!;
form.addEventListener("submit", event => event.preventDefault());</script>
<style> .otp-input-basic { display: grid; gap: 0.5em; place-items: center;
label { font-weight: 600; } }</style>Submit when complete
Verify the code as soon as the user enters the last character, without a submit button. autosubmit submits the form once the last box is filled, and the value property reads the code and clears the boxes after a wrong one.
<form class="otp-input-autosubmit"> <label for="otp-input-autosubmit-code">Enter the code we sent you</label> <sv-otp-input id="otp-input-autosubmit-code" autosubmit name="code" required></sv-otp-input> <output>The code is 123456.</output></form>
<script type="application/x-typescript" module> const form = document.querySelector<HTMLFormElement>(".otp-input-autosubmit")!; const otpInput = form.querySelector<OtpInput>("sv-otp-input")!; const status = form.querySelector("output")!;
form.addEventListener("submit", event => { event.preventDefault();
if (otpInput.value === "123456") { status.textContent = "Verified."; return; }
status.textContent = "Wrong code. Try again."; otpInput.value = ""; });</script>
<style> .otp-input-autosubmit { display: grid; gap: 0.5em; place-items: center;
label { font-weight: 600; }
output { font-size: 0.875em; color: var(--sv-gray-3); } }</style>Custom style
Restyle the boxes as underlines with a star in each empty one. The --sv-otp-input-* tokens set the box size, gap, and border, ::part(cell)::before draws the star, and ::part(cell filled) styles a box that holds a character.
<div class="otp-input-styling"> <label for="otp-input-styled-code">Verification code</label> <sv-otp-input id="otp-input-styled-code" groups="3 3"></sv-otp-input></div>
<style> .otp-input-styling { display: grid; gap: 0.5em; place-items: center;
label { font-weight: 600; }
sv-otp-input { --sv-otp-input-cell-bdr-rad: 0; --sv-otp-input-cell-bg: transparent; --sv-otp-input-cell-bdr-sz: 2px; --sv-otp-input-cell-width: 2em; --sv-otp-input-gap: 0.75em;
font-size: 1.25em; }
sv-otp-input::part(cell) { border-block-start-color: transparent; border-inline-color: transparent; }
sv-otp-input::part(cell filled) { border-block-end-color: var(--sv-accent); }
sv-otp-input::part(cell)::before { inline-size: 0.5em; aspect-ratio: 1; content: ""; background-color: var(--sv-gray-3); mask: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m12 2 3 7h7l-5.5 4.5 2 7.5-6.5-4.5L5.5 21l2-7.5L2 9h7z"/></svg>') center / contain no-repeat; }
sv-otp-input::part(cell filled)::before { content: none; } }</style>