Bootstrap 5 Email Input with Animated Underline and Validation
An email input with an animated underline, live validation and a clear success state before the subscribe button unlocks.
<section class="wdw-stage">
<div class="container" style="max-width: 680px">
<div class="wdw-card p-4 p-md-5">
<span class="wdw-eyebrow">Stay in the loop</span>
<h2 class="wdw-heading display-6 mt-2 mb-2">What's your email?</h2>
<p class="wdw-muted mb-4">One short note a month. No spam, unsubscribe anytime.</p>
<form id="wfForm" novalidate>
<div class="input-group wf-field" id="wfField">
<span class="input-group-text" aria-hidden="true"><svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="5" width="18" height="14" rx="2"/><path d="m3 7 9 6 9-6"/></svg></span>
<label for="wfEmail" class="visually-hidden">Email address</label>
<input type="email" id="wfEmail" class="form-control wf-input" placeholder="[email protected]" autocomplete="email" value="[email protected]" aria-describedby="wfHint">
<span class="input-group-text wf-state" id="wfState" aria-hidden="true"></span>
<span class="wf-line" aria-hidden="true"></span>
</div>
<div class="d-flex justify-content-between align-items-center mt-3 gap-3 flex-wrap">
<span class="small wdw-muted" id="wfHint" role="status" aria-live="polite">Looks good.</span>
<button type="submit" class="btn btn-primary rounded-pill px-4" id="wfBtn">Subscribe</button>
</div>
</form>
</div>
</div>
</section>
/* Web Designer Wall library style — shared base for every Bootstrap 5 snippet.
Indigo accent, Plus Jakarta Sans, soft cards, pill buttons. Works in light and dark. */
@import url("https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap");
:root,
[data-bs-theme="light"] {
--wdw-font: "Plus Jakarta Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
--bs-body-font-family: var(--wdw-font);
--bs-primary: #4f46e5;
--bs-primary-rgb: 79, 70, 229;
--bs-primary-text-emphasis: #3730a3;
--bs-primary-bg-subtle: #eef2ff;
--bs-primary-border-subtle: #c7d2fe;
--bs-link-color: #4f46e5;
--bs-link-color-rgb: 79, 70, 229;
--bs-link-hover-color: #4338ca;
--bs-link-hover-color-rgb: 67, 56, 202;
--bs-focus-ring-color: rgba(79, 70, 229, 0.25);
--bs-border-radius: 0.75rem;
--bs-border-radius-lg: 1rem;
--bs-border-radius-xl: 1.25rem;
--wdw-radius-card: 1.25rem;
--wdw-shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 16px 40px -16px rgba(16, 24, 40, 0.22);
--wdw-shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 12px -4px rgba(16, 24, 40, 0.12);
--wdw-stage: #f5f6fb;
}
[data-bs-theme="dark"] {
--bs-primary: #818cf8;
--bs-primary-rgb: 129, 140, 248;
--bs-primary-text-emphasis: #c7d2fe;
--bs-primary-bg-subtle: rgba(129, 140, 248, 0.14);
--bs-primary-border-subtle: rgba(129, 140, 248, 0.35);
--bs-link-color: #a5b4fc;
--bs-link-color-rgb: 165, 180, 252;
--bs-link-hover-color: #c7d2fe;
--bs-link-hover-color-rgb: 199, 210, 254;
--bs-focus-ring-color: rgba(129, 140, 248, 0.35);
--wdw-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 16px 40px -16px rgba(0, 0, 0, 0.7);
--wdw-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 12px -4px rgba(0, 0, 0, 0.5);
--wdw-stage: #0f1117;
}
body { font-family: var(--wdw-font); -webkit-font-smoothing: antialiased; }
/* Bootstrap compiles its blue into these components; point them at the accent. */
.btn-primary {
--bs-btn-bg: var(--bs-primary); --bs-btn-border-color: var(--bs-primary);
--bs-btn-hover-bg: #4338ca; --bs-btn-hover-border-color: #4338ca;
--bs-btn-active-bg: #3730a3; --bs-btn-active-border-color: #3730a3;
--bs-btn-disabled-bg: var(--bs-primary); --bs-btn-disabled-border-color: var(--bs-primary);
--bs-btn-focus-shadow-rgb: var(--bs-primary-rgb);
}
[data-bs-theme="dark"] .btn-primary { --bs-btn-color: #0f1117; --bs-btn-hover-bg: #a5b4fc; --bs-btn-hover-border-color: #a5b4fc; --bs-btn-hover-color: #0f1117; --bs-btn-active-color: #0f1117; }
.btn-outline-primary {
--bs-btn-color: var(--bs-primary); --bs-btn-border-color: var(--bs-primary);
--bs-btn-hover-bg: var(--bs-primary); --bs-btn-hover-border-color: var(--bs-primary);
--bs-btn-active-bg: var(--bs-primary); --bs-btn-active-border-color: var(--bs-primary);
--bs-btn-focus-shadow-rgb: var(--bs-primary-rgb);
}
.btn { --bs-btn-font-weight: 600; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: .5rem; }
.btn-sm { min-height: 36px; }
.form-control, .form-select { min-height: 44px; border-radius: .75rem; }
.form-control:focus, .form-select:focus, .form-check-input:focus { border-color: rgba(var(--bs-primary-rgb), .6); box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.form-check-input:checked { background-color: var(--bs-primary); border-color: var(--bs-primary); }
.nav-pills { --bs-nav-pills-link-active-bg: var(--bs-primary); }
.nav { --bs-nav-link-color: var(--bs-secondary-color); --bs-nav-link-hover-color: var(--bs-emphasis-color); }
.pagination { --bs-pagination-active-bg: var(--bs-primary); --bs-pagination-active-border-color: var(--bs-primary); --bs-pagination-color: var(--bs-body-color); --bs-pagination-focus-box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.progress, .progress-stacked { --bs-progress-bar-bg: var(--bs-primary); }
.list-group { --bs-list-group-active-bg: var(--bs-primary); --bs-list-group-active-border-color: var(--bs-primary); }
.dropdown-menu { --bs-dropdown-link-active-bg: var(--bs-primary); border-radius: .875rem; box-shadow: var(--wdw-shadow-sm); }
.accordion { --bs-accordion-active-bg: var(--bs-primary-bg-subtle); --bs-accordion-active-color: var(--bs-primary-text-emphasis); --bs-accordion-btn-focus-box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.badge { font-weight: 600; }
/* Library building blocks — use these instead of re-inventing them per snippet. */
.wdw-stage { min-height: 100vh; display: flex; align-items: center; background: var(--wdw-stage); padding-block: 3rem; }
.wdw-card { background: var(--bs-body-bg); border: 1px solid var(--bs-border-color-translucent); border-radius: var(--wdw-radius-card); box-shadow: var(--wdw-shadow); }
.wdw-card-sm { background: var(--bs-body-bg); border: 1px solid var(--bs-border-color-translucent); border-radius: 1rem; box-shadow: var(--wdw-shadow-sm); }
.wdw-aurora { position: relative; color: #fff; background: radial-gradient(60% 55% at 100% 0, #818cf8 0, transparent 70%), radial-gradient(65% 60% at 0 100%, #ec4899 0, transparent 65%), #1e1b4b; }
.wdw-eyebrow { display: inline-block; font-size: .75rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--bs-primary); }
.wdw-heading { font-weight: 800; letter-spacing: -.02em; color: var(--bs-emphasis-color); }
.wdw-muted { color: var(--bs-secondary-color); }
.wdw-icon { width: 2.75rem; height: 2.75rem; flex: none; display: inline-flex; align-items: center; justify-content: center; border-radius: .875rem; background: var(--bs-primary-bg-subtle); color: var(--bs-primary); }
/* Component */
.wf-field { position: relative; flex-wrap: nowrap; min-height: 68px; background: var(--bs-tertiary-bg); border-radius: .875rem .875rem 0 0; color: var(--bs-secondary-color); transition: background .2s, color .2s; }
.wf-field > .input-group-text, .wf-field > .form-control { background: transparent; border: 0; border-radius: 0; color: inherit; }
.wf-field > .input-group-text { padding-inline: 1rem .25rem; }
.wf-field > .form-control { font-size: 1.125rem; font-weight: 500; color: var(--bs-emphasis-color); padding: .75rem .5rem; box-shadow: none; }
.wf-field > .form-control::placeholder { color: var(--bs-secondary-color); opacity: .8; }
.wf-field:focus-within { background: var(--bs-primary-bg-subtle); color: var(--bs-primary); outline: 2px solid var(--bs-primary); outline-offset: 2px; }
.wf-line { position: absolute; left: 0; right: 0; bottom: 0; height: 2px; background: var(--bs-border-color); z-index: 4; }
.wf-line::after { content: ""; position: absolute; inset: 0; background: var(--bs-primary); transform: scaleX(0); transform-origin: left; transition: transform .35s ease, background .2s; }
.wf-field:focus-within .wf-line::after { transform: scaleX(1); }
.wf-field.is-valid .wf-line::after { transform: scaleX(1); background: var(--bs-success); }
.wf-field.is-invalid .wf-line::after { transform: scaleX(1); background: var(--bs-danger); }
.wf-state { width: 46px; padding-inline: 0 1rem; }
.wf-field.is-valid .wf-state::before, .wf-field.is-invalid .wf-state::before { content: ""; display: block; width: 22px; height: 22px; border-radius: 50%; background-color: var(--bs-success); background-position: center; background-repeat: no-repeat; background-size: 14px; }
.wf-field.is-valid .wf-state::before { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='3.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m5 12.5 4.5 4.5L19 7.5'/%3E%3C/svg%3E"); }
.wf-field.is-invalid .wf-state::before { background-color: var(--bs-danger); background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='3.5' stroke-linecap='round'%3E%3Cpath d='M12 5v8M12 19v.01'/%3E%3C/svg%3E"); }
.wf-field.is-valid { color: var(--bs-success); }
.wf-field.is-invalid { color: var(--bs-danger); }
#wfHint.is-error { color: var(--bs-danger-text-emphasis) !important; font-weight: 600; }
#wfBtn:disabled { background: var(--bs-secondary-bg); border-color: var(--bs-border-color); color: var(--bs-secondary-color); opacity: 1; }
@media (prefers-reduced-motion: reduce) { .wf-line::after { transition: none; } }
(function () {
function initEmailField() {
var form = document.getElementById('wfForm');
var input = document.getElementById('wfEmail');
var field = document.getElementById('wfField');
var hint = document.getElementById('wfHint');
var btn = document.getElementById('wfBtn');
if (!form || !input || !field || !hint || !btn) return;
var re = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
function setState(state, msg) {
field.classList.toggle('is-valid', state === 'valid');
field.classList.toggle('is-invalid', state === 'invalid');
hint.classList.toggle('is-error', state === 'invalid');
if (state === 'invalid') input.setAttribute('aria-invalid', 'true'); else input.removeAttribute('aria-invalid');
hint.textContent = msg;
btn.disabled = state !== 'valid';
}
function check(onBlur) {
var v = input.value.trim();
if (re.test(v)) setState('valid', 'Looks good.');
else if (onBlur && v) setState('invalid', 'That email does not look right.');
else setState('', v ? 'Keep typing, we need a full address.' : 'Type your address to get started.');
}
input.addEventListener('input', function () { check(false); });
input.addEventListener('blur', function () { check(true); });
form.addEventListener('submit', function (e) {
e.preventDefault();
if (!re.test(input.value.trim())) return;
hint.textContent = 'Thanks, you are subscribed.';
btn.disabled = true;
btn.textContent = 'Subscribed';
input.readOnly = true;
});
check(false);
}
initEmailField();
})();
Built on Bootstrap 5.3.8 with plain JavaScript — no jQuery. Supports light and dark mode via data-bs-theme.