Bootstrap 5 Credit Card Payment Form with Live Preview
A credit card payment form with a live card preview, floating labels, input formatting and inline validation alerts.
<section class="wdw-stage">
<div class="container" style="max-width:960px">
<div class="row g-4 align-items-center">
<div class="col-lg-5">
<span class="wdw-eyebrow">Secure checkout</span>
<h2 class="wdw-heading display-6 mt-1 mb-3">Pay by card</h2>
<p class="wdw-muted mb-4">Your details are encrypted. Watch the card update as you type.</p>
<div class="wdw-aurora pay-card rounded-4 p-4 d-flex flex-column justify-content-between" aria-hidden="true">
<div class="d-flex justify-content-between align-items-center">
<svg width="38" height="28" viewBox="0 0 38 28" fill="none"><rect x=".75" y=".75" width="36.5" height="26.5" rx="5" stroke="#fff" stroke-opacity=".6" stroke-width="1.5"/><path d="M1 10h36M12 10v17.5M26 10v17.5M1 19h36" stroke="#fff" stroke-opacity=".6" stroke-width="1.2"/></svg>
<span class="small fw-semibold opacity-75">Credit</span>
</div>
<div class="pay-number fs-4 fw-semibold" id="pvNumber">•••• •••• •••• ••••</div>
<div class="d-flex justify-content-between small">
<div><div class="opacity-50 text-uppercase pay-lbl">Card holder</div><div class="fw-semibold" id="pvName">Your name</div></div>
<div class="text-end"><div class="opacity-50 text-uppercase pay-lbl">Expires</div><div class="fw-semibold" id="pvExp">MM/YY</div></div>
</div>
</div>
</div>
<div class="col-lg-7">
<form class="wdw-card p-4 p-md-5" id="payForm" novalidate>
<div class="alert alert-danger d-flex align-items-center justify-content-between py-2 mb-4" id="payAlert" role="alert">
<span id="payAlertMsg">CVC code is required.</span>
<button type="button" class="btn-close" aria-label="Dismiss" data-pay-dismiss></button>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="payName" placeholder="Name" autocomplete="cc-name" required>
<label for="payName">Card holder name</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="payNumber" placeholder="Number" inputmode="numeric" autocomplete="cc-number" maxlength="19" required>
<label for="payNumber">Card number</label>
</div>
<div class="row g-3 mb-4">
<div class="col-6">
<div class="form-floating">
<input type="text" class="form-control" id="payExp" placeholder="MM/YY" inputmode="numeric" autocomplete="cc-exp" maxlength="5" required>
<label for="payExp">Expiry (MM/YY)</label>
</div>
</div>
<div class="col-6">
<div class="form-floating">
<input type="text" class="form-control" id="payCvc" placeholder="CVC" inputmode="numeric" autocomplete="cc-csc" maxlength="4" required>
<label for="payCvc">CVC</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary rounded-pill w-100">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></svg>
Pay $149.00
</button>
<p class="text-center wdw-muted small mt-3 mb-0">Payments are processed securely. We never store your card.</p>
</form>
</div>
</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 */
.pay-card { aspect-ratio: 1.586; max-width: 380px; box-shadow: var(--wdw-shadow); overflow: hidden; }
.pay-number { letter-spacing: .12em; font-variant-numeric: tabular-nums; }
.pay-lbl { font-size: .625rem; letter-spacing: .08em; }
function initPaymentForm() {
const form = document.getElementById('payForm');
if (!form) return;
const byId = id => document.getElementById(id);
const num = byId('payNumber'), name = byId('payName'), exp = byId('payExp'), cvc = byId('payCvc');
const alertBox = byId('payAlert'), msg = byId('payAlertMsg');
const bullets = '•••• •••• •••• ••••';
num.addEventListener('input', () => {
num.value = num.value.replace(/\D/g, '').slice(0, 16).replace(/(.{4})/g, '$1 ').trim();
const pad = (num.value + bullets.slice(num.value.length)).slice(0, 19);
byId('pvNumber').textContent = pad;
});
name.addEventListener('input', () => { byId('pvName').textContent = name.value.trim() || 'Your name'; });
exp.addEventListener('input', () => {
let v = exp.value.replace(/\D/g, '').slice(0, 4);
if (v.length > 2) v = v.slice(0, 2) + '/' + v.slice(2);
exp.value = v;
byId('pvExp').textContent = v || 'MM/YY';
});
cvc.addEventListener('input', () => { cvc.value = cvc.value.replace(/\D/g, ''); });
const checks = [
[name, v => v.trim().length > 1, 'Card holder name is required.'],
[num, v => v.replace(/\s/g, '').length === 16, 'Enter a valid 16 digit card number.'],
[exp, v => /^(0[1-9]|1[0-2])\/\d{2}$/.test(v), 'Enter an expiry date as MM/YY.'],
[cvc, v => /^\d{3,4}$/.test(v), 'CVC code is required.']
];
form.addEventListener('submit', e => {
e.preventDefault();
let first = null;
checks.forEach(([el, ok, text]) => {
const bad = !ok(el.value);
el.classList.toggle('is-invalid', bad);
el.setAttribute('aria-invalid', bad);
if (bad && !first) first = [el, text];
});
alertBox.classList.toggle('d-none', !first);
if (first) { msg.textContent = first[1]; first[0].focus(); }
else { alertBox.className = 'alert alert-success py-2 mb-4'; msg.textContent = 'Payment successful. Thank you!'; }
});
form.querySelector('[data-pay-dismiss]').addEventListener('click', () => { alertBox.classList.add('d-none'); });
}
initPaymentForm();
Built on Bootstrap 5.3.8 with plain JavaScript — no jQuery. Supports light and dark mode via data-bs-theme.