Verification code with digit display in HTML, CSS and vanilla JS
A copy-paste Verification code with digit display in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="dcl-code-entry-display" data-dcl-scheme="light" aria-label="Verification code with digit display example">
<form>
<h2>Verify the six digits</h2><p id="dcl-code-entry-display-hint">Enter sample code <strong>482193</strong>.</p>
<div data-digits aria-hidden="true"><span data-well>–</span><span data-well>–</span><span data-well>–</span><span data-well>–</span><span data-well>–</span><span data-well>–</span></div>
<label>Sample code<input data-code required inputmode="numeric" autocomplete="one-time-code" pattern="[0-9]{6}" maxlength="6" aria-describedby="dcl-code-entry-display-hint"></label>
<div><button type="submit" data-primary>Check sample</button></div><p role="status"></p>
</form>
<script>
(() => {
const root = document.currentScript.closest(".dcl-code-entry-display");
const instance = Array.from(document.getElementsByClassName("dcl-code-entry-display")).indexOf(root);
if (instance > 0) {
const ids = new Map();
for (const element of root.querySelectorAll("[id]")) {
ids.set(element.id, element.id + "--" + instance);
element.id = ids.get(element.id);
}
for (const element of root.querySelectorAll("[aria-labelledby], [aria-describedby], [aria-controls], [for], [href]")) {
for (const attribute of ["aria-labelledby", "aria-describedby", "aria-controls", "for"]) {
if (!element.hasAttribute(attribute)) continue;
const updated = element.getAttribute(attribute).split(/\s+/).map((id) => ids.get(id) || id);
element.setAttribute(attribute, updated.join(" "));
}
const href = element.getAttribute("href");
if (href && href.startsWith("#") && ids.has(href.slice(1))) element.setAttribute("href", "#" + ids.get(href.slice(1)));
}
}
const preference = matchMedia("(prefers-color-scheme: dark)");
const setTheme = () => { root.dataset.dclScheme = preference.matches ? "dark" : "light"; };
preference.addEventListener("change", setTheme);
setTheme();
const find = (selector) => root.querySelector(selector);
const all = (selector) => Array.from(root.querySelectorAll(selector));
function onSubmit(form, action) {
const submit = form.querySelector('[type="submit"]');
// A sandboxed preview blocks form submission, but still dispatches the submit button's click.
submit.addEventListener("click", (event) => {
event.preventDefault();
if (form.reportValidity()) action();
});
form.addEventListener("submit", (event) => {
event.preventDefault();
action();
});
}
const form = find("form");
const field = form.querySelector("[data-code]");
const response = find('[role="status"]');
onSubmit(form, () => {
const valid = field.value === "482193";
field.setAttribute("aria-invalid", String(!valid));
response.textContent = valid ? "Sample code accepted. No authentication occurred." : "That sample does not match. Check the example above.";
if (!valid) field.focus();
});
field.addEventListener("input", () => {
field.removeAttribute("aria-invalid");
response.textContent = "";
});
field.addEventListener("input", () => {
all("[data-digits] span").forEach((digit, index) => {
digit.textContent = field.value[index] || "–";
});
});
})();
</script>
</section>
<style>
.dcl-code-entry-display, .dcl-code-entry-display * { box-sizing: border-box; }
.dcl-code-entry-display { font: 1rem/1.5 system-ui, sans-serif; max-inline-size: 34rem; padding: clamp(0.75rem, 3vw, 1.25rem); border: 1px solid; border-radius: 0.75rem; overflow-wrap: anywhere; }
.dcl-code-entry-display :where(h2, h3, p, ol, ul, dl, dd) { margin: 0; }
.dcl-code-entry-display h2 { font-size: 1.25rem; line-height: 1.25; }
.dcl-code-entry-display h3 { font-size: 1rem; }
.dcl-code-entry-display p + p { margin-block-start: 0.5rem; }
.dcl-code-entry-display [data-stack], .dcl-code-entry-display form { display: grid; gap: 0.75rem; }
.dcl-code-entry-display [data-row] { display: flex; flex-wrap: wrap; gap: 0.5rem 0.75rem; align-items: center; }
.dcl-code-entry-display [data-split] { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr)); gap: 0.75rem 1.25rem; align-items: start; }
.dcl-code-entry-display [data-spread] { justify-content: space-between; }
.dcl-code-entry-display [hidden] { display: none !important; }
.dcl-code-entry-display [data-well] { padding: 0.75rem; border-radius: 0.375rem; }
.dcl-code-entry-display [data-chip] { display: inline-block; width: fit-content; padding: 0.125rem 0.55rem; border-radius: 99rem; font-size: 0.875rem; font-weight: 700; }
.dcl-code-entry-display :where(button, summary, input, select) { font: inherit; }
.dcl-code-entry-display :where(button, input, select) { min-block-size: 44px; min-inline-size: 0; max-inline-size: 100%; }
.dcl-code-entry-display :where(button, input, select) { border: 1px solid; border-radius: 0.375rem; padding: 0.45rem 0.7rem; }
.dcl-code-entry-display :where(button, summary) { cursor: pointer; font-weight: 650; }
.dcl-code-entry-display :where(button, summary):active { box-shadow: inset 0 0 0 2px currentColor; }
.dcl-code-entry-display summary:hover { text-decoration: underline; }
.dcl-code-entry-display :where(button, input, select):disabled, .dcl-code-entry-display [aria-disabled=true] { cursor: not-allowed; border-style: dashed; }
.dcl-code-entry-display label { display: grid; gap: 0.25rem; }
.dcl-code-entry-display :where(input, select) { inline-size: 100%; }
.dcl-code-entry-display label[data-choice] { display: flex; gap: 0.5rem; align-items: center; min-block-size: 44px; }
.dcl-code-entry-display label[data-choice] input { inline-size: 1.25rem; block-size: 1.25rem; min-block-size: 0; flex-shrink: 0; }
.dcl-code-entry-display fieldset { min-inline-size: 0; margin: 0; border-radius: 0.375rem; padding: 0.5rem 0.75rem; }
.dcl-code-entry-display :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-code-entry-display[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.dcl-code-entry-display [data-digits] { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 0.25rem; text-align: center; }
.dcl-code-entry-display [data-digits] span { padding: 0.4rem 0; font-variant-numeric: tabular-nums; }
.dcl-code-entry-display { color-scheme: light; border-color: #697f84; }
.dcl-code-entry-display { color: #11292f; background-color: #ffffff; }
.dcl-code-entry-display [data-well], .dcl-code-entry-display [data-chip] { color: #11292f; background-color: #e4edef; }
.dcl-code-entry-display button, .dcl-code-entry-display input, .dcl-code-entry-display select { color: #11292f; background-color: #e4edef; }
.dcl-code-entry-display button[data-primary] { color: #ffffff; background-color: #00535a; }
.dcl-code-entry-display button:hover { color: #ffffff; background-color: #003c42; }
.dcl-code-entry-display button[disabled], .dcl-code-entry-display button[aria-disabled=true] { color: #405a60; background-color: #e4edef; }
.dcl-code-entry-display[data-dcl-scheme="dark"] { color-scheme: dark; border-color: #839fa5; }
.dcl-code-entry-display[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; }
.dcl-code-entry-display[data-dcl-scheme="dark"] [data-well], .dcl-code-entry-display[data-dcl-scheme="dark"] [data-chip] { color: #eff8f9; background-color: #1c3c44; }
.dcl-code-entry-display[data-dcl-scheme="dark"] button, .dcl-code-entry-display[data-dcl-scheme="dark"] input, .dcl-code-entry-display[data-dcl-scheme="dark"] select { color: #eff8f9; background-color: #1c3c44; }
.dcl-code-entry-display[data-dcl-scheme="dark"] button[data-primary] { color: #11292f; background-color: #8fe1da; }
.dcl-code-entry-display[data-dcl-scheme="dark"] button:hover { color: #11292f; background-color: #b2f2ec; }
.dcl-code-entry-display[data-dcl-scheme="dark"] button[disabled], .dcl-code-entry-display[data-dcl-scheme="dark"] button[aria-disabled=true] { color: #b0c9ce; background-color: #1c3c44; }
</style>
When to use
Show progress in six visual boxes without segmenting the underlying field.
Avoid
Do not create six independent tab stops or suppress native paste.
Accessibility
The six boxes are decorative. The single field stays labeled for paste and keyboard entry. Text contrast is at least 6.1:1 in light and 6.7:1 in dark.
Was this useful? Send feedback.