Component Library

← Back to gallery

Email verification address correction in HTML, CSS and vanilla JS

A copy-paste Email verification address correction in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Authentication · Tags: email, verification, correction, contact

Live preview

Source (copy-ready)

<section class="dcl-verify-contact-change" data-dcl-scheme="light" aria-label="Email verification address correction example">
<div data-stack><h2>Is the verification address correct?</h2>
  <p data-well>Current sample destination: member@example.com</p>
  <div><button type="button" data-edit aria-expanded="false" aria-controls="dcl-verify-contact-change-edit">Change sample address</button></div>
  <form id="dcl-verify-contact-change-edit" hidden><label>New sample email<input type="email" autocomplete="email" required></label><div><button type="submit" data-primary>Review new address</button></div></form>
  <p role="status"></p>
</div>
<script>
(() => {
  const root = document.currentScript.closest(".dcl-verify-contact-change");
  const instance = Array.from(document.getElementsByClassName("dcl-verify-contact-change")).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);
  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 edit = find("[data-edit]");
  const form = find("form");
  const input = find("input");
  function show(visible) {
    form.hidden = !visible;
    edit.setAttribute("aria-expanded", String(visible));
    edit.textContent = visible ? "Cancel address change" : "Change sample address";
    (visible ? input : edit).focus();
  }
  edit.addEventListener("click", () => show(form.hidden));
  form.addEventListener("keydown", (event) => { if (event.key === "Escape") show(false); });
  onSubmit(form, () => {
    find('[role="status"]').textContent = "New address previewed: " + input.value + ". Nothing was saved or sent.";
    show(false);
  });
})();
</script>
</section>
<style>
.dcl-verify-contact-change, .dcl-verify-contact-change * { box-sizing: border-box; }
.dcl-verify-contact-change { 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-verify-contact-change :where(h2, h3, p, ol, ul, dl, dd) { margin: 0; }
.dcl-verify-contact-change h2 { font-size: 1.25rem; line-height: 1.25; }
.dcl-verify-contact-change h3 { font-size: 1rem; }
.dcl-verify-contact-change p + p { margin-block-start: 0.5rem; }
.dcl-verify-contact-change [data-stack], .dcl-verify-contact-change form { display: grid; gap: 0.75rem; }
.dcl-verify-contact-change [data-row] { display: flex; flex-wrap: wrap; gap: 0.5rem 0.75rem; align-items: center; }
.dcl-verify-contact-change [data-split] { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr)); gap: 0.75rem 1.25rem; align-items: start; }
.dcl-verify-contact-change [data-spread] { justify-content: space-between; }
.dcl-verify-contact-change [hidden] { display: none !important; }
.dcl-verify-contact-change [data-well] { padding: 0.75rem; border-radius: 0.375rem; }
.dcl-verify-contact-change [data-chip] { display: inline-block; width: fit-content; padding: 0.125rem 0.55rem; border-radius: 99rem; font-size: 0.875rem; font-weight: 700; }
.dcl-verify-contact-change :where(button, summary, input, select) { font: inherit; }
.dcl-verify-contact-change :where(button, input, select) { min-block-size: 44px; min-inline-size: 0; max-inline-size: 100%; }
.dcl-verify-contact-change :where(button, input, select) { border: 1px solid; border-radius: 0.375rem; padding: 0.45rem 0.7rem; }
.dcl-verify-contact-change :where(button, summary) { cursor: pointer; font-weight: 650; }
.dcl-verify-contact-change :where(button, summary):active { box-shadow: inset 0 0 0 2px currentColor; }
.dcl-verify-contact-change summary:hover { text-decoration: underline; }
.dcl-verify-contact-change :where(button, input, select):disabled, .dcl-verify-contact-change [aria-disabled=true] { cursor: not-allowed; border-style: dashed; }
.dcl-verify-contact-change label { display: grid; gap: 0.25rem; }
.dcl-verify-contact-change :where(input, select) { inline-size: 100%; }
.dcl-verify-contact-change label[data-choice] { display: flex; gap: 0.5rem; align-items: center; min-block-size: 44px; }
.dcl-verify-contact-change label[data-choice] input { inline-size: 1.25rem; block-size: 1.25rem; min-block-size: 0; flex-shrink: 0; }
.dcl-verify-contact-change fieldset { min-inline-size: 0; margin: 0; border-radius: 0.375rem; padding: 0.5rem 0.75rem; }
.dcl-verify-contact-change :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-verify-contact-change[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.dcl-verify-contact-change { color-scheme: light; border-color: #697f84; }
.dcl-verify-contact-change { color: #11292f; background-color: #ffffff; }
.dcl-verify-contact-change [data-well], .dcl-verify-contact-change [data-chip] { color: #11292f; background-color: #e4edef; }
.dcl-verify-contact-change button, .dcl-verify-contact-change input, .dcl-verify-contact-change select { color: #11292f; background-color: #e4edef; }
.dcl-verify-contact-change button[data-primary] { color: #ffffff; background-color: #00535a; }
.dcl-verify-contact-change button:hover { color: #ffffff; background-color: #003c42; }
.dcl-verify-contact-change button[disabled], .dcl-verify-contact-change button[aria-disabled=true] { color: #405a60; background-color: #e4edef; }
.dcl-verify-contact-change[data-dcl-scheme="dark"] { color-scheme: dark; border-color: #839fa5; }
.dcl-verify-contact-change[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; }
.dcl-verify-contact-change[data-dcl-scheme="dark"] [data-well], .dcl-verify-contact-change[data-dcl-scheme="dark"] [data-chip] { color: #eff8f9; background-color: #1c3c44; }
.dcl-verify-contact-change[data-dcl-scheme="dark"] button, .dcl-verify-contact-change[data-dcl-scheme="dark"] input, .dcl-verify-contact-change[data-dcl-scheme="dark"] select { color: #eff8f9; background-color: #1c3c44; }
.dcl-verify-contact-change[data-dcl-scheme="dark"] button[data-primary] { color: #11292f; background-color: #8fe1da; }
.dcl-verify-contact-change[data-dcl-scheme="dark"] button:hover { color: #11292f; background-color: #b2f2ec; }
.dcl-verify-contact-change[data-dcl-scheme="dark"] button[disabled], .dcl-verify-contact-change[data-dcl-scheme="dark"] button[aria-disabled=true] { color: #b0c9ce; background-color: #1c3c44; }
</style>

When to use

Offer address correction when the member may have mistyped their contact email.

Avoid

Do not overwrite an already verified address until the server verifies the new one.

Accessibility

The opening control exposes its expanded state. Escape closes the form and returns focus to that control. Text contrast is at least 6.1:1 in light and 6.7:1 in dark.

Was this useful? Send feedback.