Bootstrap 5 Credit Card Checkout with Formatted Input
A credit card checkout with a live card preview that highlights the active field, brand-aware number formatting, an itemised receipt-style order summary and a promo code field.
<section class="wdw-stage cc-stage">
<div class="container" style="max-width:1020px">
<form class="wdw-card overflow-hidden" id="cc-form" novalidate>
<div class="row g-0">
<div class="col-lg-7 p-4 p-md-5">
<span class="wdw-eyebrow mb-1">Checkout</span>
<h2 class="wdw-heading h4 mb-4">Payment details</h2>
<div class="row g-3">
<div class="col-12"><div class="form-floating"><input class="form-control" id="cc-name" placeholder="Name on card" autocomplete="cc-name" aria-describedby="cc-name-err" required><label for="cc-name">Name on card</label><div class="invalid-feedback" id="cc-name-err">Enter the name shown on the card.</div></div></div>
<div class="col-12"><div class="form-floating"><input class="form-control" id="cc-no" placeholder="Card number" inputmode="numeric" autocomplete="cc-number" maxlength="19" aria-describedby="cc-no-err" required><label for="cc-no">Card number</label><div class="invalid-feedback" id="cc-no-err">Enter a valid card number.</div></div></div>
<div class="col-6"><div class="form-floating"><input class="form-control" id="cc-exp" placeholder="MM/YY" inputmode="numeric" autocomplete="cc-exp" maxlength="5" aria-describedby="cc-exp-err" required><label for="cc-exp">Expiry (MM/YY)</label><div class="invalid-feedback" id="cc-exp-err">Use MM/YY.</div></div></div>
<div class="col-6"><div class="form-floating"><input class="form-control" id="cc-cvc" placeholder="CVC" inputmode="numeric" autocomplete="cc-csc" maxlength="4" aria-describedby="cc-cvc-err" required><label for="cc-cvc">CVC</label><div class="invalid-feedback" id="cc-cvc-err">Enter the security code.</div></div></div>
<div class="col-12"><div class="form-floating"><input type="email" class="form-control" id="cc-mail" placeholder="Receipt email" autocomplete="email" aria-describedby="cc-mail-err" required><label for="cc-mail">Email for receipt</label><div class="invalid-feedback" id="cc-mail-err">Enter a valid email address.</div></div></div>
<div class="col-sm-6"><div class="form-floating"><select class="form-select" id="cc-country" autocomplete="country"><option>United States</option><option>Canada</option><option>United Kingdom</option><option>Germany</option><option>Australia</option></select><label for="cc-country">Country</label></div></div>
<div class="col-sm-6"><div class="form-floating"><input class="form-control" id="cc-zip" placeholder="Postal code" autocomplete="postal-code" aria-describedby="cc-zip-err" required><label for="cc-zip">Postal code</label><div class="invalid-feedback" id="cc-zip-err">Enter your postal code.</div></div></div>
<div class="col-12"><div class="form-check"><input class="form-check-input" type="checkbox" id="cc-save" checked><label class="form-check-label" for="cc-save">Save this card for future payments</label></div></div>
</div>
<p class="small wdw-muted d-flex gap-2 align-items-center mt-4 mb-0"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></svg>Payments are encrypted and secure.</p>
</div>
<div class="col-lg-5 cc-side p-4 p-md-5">
<div class="cc-preview wdw-aurora rounded-4 p-3 mb-4" aria-hidden="true">
<div class="d-flex justify-content-between align-items-start">
<svg width="34" height="26" viewBox="0 0 34 26" fill="none"><rect x="1" y="1" width="32" height="24" rx="5" fill="#fff" fill-opacity=".18" stroke="#fff" stroke-opacity=".7" stroke-width="1.5"/><path d="M1 9h32M1 17h32M12 1v24" stroke="#fff" stroke-opacity=".6" stroke-width="1.5"/></svg>
<span class="cc-brand" id="cc-brand" data-brand="card"><span class="cc-b cc-b-visa">VISA</span><svg class="cc-b cc-b-mc" width="40" height="26" viewBox="0 0 40 26"><circle cx="14" cy="13" r="12" fill="#eb001b"/><circle cx="26" cy="13" r="12" fill="#f79e1b" fill-opacity=".9"/></svg><span class="cc-b cc-b-amex">AMEX</span></span>
</div>
<div class="cc-num mt-3 mb-2" id="cc-p-num">•••• •••• •••• ••••</div>
<div class="d-flex justify-content-between align-items-end small text-uppercase gap-2">
<span id="cc-p-name" class="cc-f text-truncate" data-for="cc-name">Your name</span>
<span class="d-flex gap-3"><span id="cc-p-exp" class="cc-f" data-for="cc-exp">MM/YY</span><span class="cc-f" data-for="cc-cvc">CVC <span id="cc-p-cvc">•••</span></span></span>
</div>
</div>
<h3 class="h5 fw-bold mb-3">Order summary</h3>
<dl class="mb-3 cc-receipt">
<div class="d-flex justify-content-between align-items-center mb-2"><dt class="fw-normal d-flex align-items-center gap-2"><span class="badge rounded-pill text-bg-primary">Pro</span>Pro plan, monthly</dt><dd class="mb-0">$49.00</dd></div>
<div class="d-flex justify-content-between mb-2"><dt class="fw-normal wdw-muted">Extra seat</dt><dd class="mb-0">$8.00</dd></div>
<div class="d-flex justify-content-between mb-2 d-none" id="cc-disc-row"><dt class="fw-normal text-success">Promo SAVE10</dt><dd class="mb-0 text-success" id="cc-disc">-$5.70</dd></div>
<div class="d-flex justify-content-between mb-2"><dt class="fw-normal wdw-muted">Tax (8%)</dt><dd class="mb-0" id="cc-tax">$4.56</dd></div>
<div class="d-flex justify-content-between align-items-baseline border-top pt-3 mt-3"><dt class="fw-semibold">Total</dt><dd class="mb-0 fs-3 fw-bold text-body-emphasis" id="cc-total">$61.56</dd></div>
</dl>
<label for="cc-promo" class="visually-hidden">Promo code</label>
<div class="input-group mb-1">
<input class="form-control" id="cc-promo" placeholder="Promo code (try SAVE10)" autocomplete="off">
<button class="btn btn-outline-primary" type="button" id="cc-apply">Apply</button>
</div>
<div class="small mb-3" id="cc-promo-msg" role="status" aria-live="polite"> </div>
<button type="submit" class="btn btn-primary rounded-pill w-100 " id="cc-pay">Pay $61.56</button>
<div class="alert alert-success d-none mt-3 mb-0 py-2" id="cc-ok" role="status">Payment successful. A receipt is on its way.</div>
</div>
</div>
</form>
</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 */
.cc-stage{padding-block:1.5rem}
.cc-side{background:var(--bs-tertiary-bg);border-left:1px solid var(--bs-border-color-translucent)}
@media (max-width:991.98px){.cc-side{border-left:0;border-top:1px solid var(--bs-border-color-translucent)}}
.cc-preview{aspect-ratio:1.75;display:flex;flex-direction:column;justify-content:space-between;box-shadow:0 18px 40px -18px rgba(30,27,75,.6)}
.cc-num{font-size:1.2rem;letter-spacing:.1em;font-variant-numeric:tabular-nums;font-weight:600;white-space:nowrap}
.cc-f{padding:.1rem .35rem;margin:-.1rem -.35rem;border-radius:.4rem;transition:background-color .15s,box-shadow .15s}
.cc-f.is-active,.cc-num.is-active{background:rgba(255,255,255,.2);box-shadow:0 0 0 1px rgba(255,255,255,.55)}
.cc-num.is-active{padding:.1rem .35rem;margin-inline:-.35rem}
.cc-b{display:none;font-weight:800;font-style:italic;font-size:1.2rem;letter-spacing:.02em}
.cc-brand[data-brand="visa"] .cc-b-visa,.cc-brand[data-brand="mc"] .cc-b-mc,.cc-brand[data-brand="amex"] .cc-b-amex{display:inline-block}
.cc-brand[data-brand="amex"] .cc-b-amex{font-style:normal;font-size:.95rem;border:2px solid #fff;border-radius:.3rem;padding:0 .35rem}
.cc-receipt dd,.cc-receipt dt{font-size:.9375rem}
@media (max-width:400px){.cc-num{font-size:.95rem}}
function initCheckout() {
var form = document.getElementById('cc-form');
if (!form) return;
var no = document.getElementById('cc-no');
var exp = document.getElementById('cc-exp');
var cvc = document.getElementById('cc-cvc');
var name = document.getElementById('cc-name');
var pNum = document.getElementById('cc-p-num');
function digits(v) { return v.replace(/\D/g, ''); }
function brand(n) {
if (/^4/.test(n)) return 'visa';
if (/^(5[1-5]|2[2-7])/.test(n)) return 'mc';
if (/^3[47]/.test(n)) return 'amex';
return 'card';
}
// Amex groups 4-6-5 (15 digits); other cards 4-4-4-4 (16 digits).
function format(d, b) {
if (b === 'amex') return [d.slice(0, 4), d.slice(4, 10), d.slice(10, 15)].filter(Boolean).join(' ');
return d.slice(0, 16).replace(/(.{4})/g, '$1 ').trim();
}
function preview() {
var d = digits(no.value), b = brand(d);
var len = b === 'amex' ? 15 : 16;
var padded = (d + '#'.repeat(len)).slice(0, len).replace(/#/g, '\u2022');
pNum.textContent = format(padded, b);
document.getElementById('cc-p-name').textContent = name.value || 'Your name';
document.getElementById('cc-p-exp').textContent = exp.value || 'MM/YY';
document.getElementById('cc-p-cvc').textContent = cvc.value || (b === 'amex' ? '\u2022\u2022\u2022\u2022' : '\u2022\u2022\u2022');
document.getElementById('cc-brand').dataset.brand = b;
}
function validateCard() {
var d = digits(no.value), b = brand(d);
no.setCustomValidity(d.length === (b === 'amex' ? 15 : 16) ? '' : 'invalid');
var need = b === 'amex' ? 4 : 3;
cvc.maxLength = need;
cvc.value = cvc.value.slice(0, need);
cvc.setCustomValidity(cvc.value.length === need ? '' : 'invalid');
document.getElementById('cc-cvc-err').textContent = 'Enter the ' + need + '-digit security code.';
}
no.addEventListener('input', function () {
var d = digits(no.value), b = brand(d);
no.maxLength = b === 'amex' ? 17 : 19;
no.value = format(d.slice(0, b === 'amex' ? 15 : 16), b);
validateCard();
preview();
});
exp.addEventListener('input', function () {
var d = digits(exp.value).slice(0, 4);
exp.value = d.length > 2 ? d.slice(0, 2) + '/' + d.slice(2) : d;
var m = parseInt(d.slice(0, 2), 10);
exp.setCustomValidity(d.length === 4 && m >= 1 && m <= 12 ? '' : 'invalid');
preview();
});
cvc.addEventListener('input', function () {
cvc.value = digits(cvc.value);
validateCard();
preview();
});
name.addEventListener('input', preview);
// Highlight the matching spot on the card preview while a field has focus.
var spots = { 'cc-no': pNum, 'cc-name': document.querySelector('[data-for="cc-name"]'), 'cc-exp': document.querySelector('[data-for="cc-exp"]'), 'cc-cvc': document.querySelector('[data-for="cc-cvc"]') };
Object.keys(spots).forEach(function (id) {
var input = document.getElementById(id);
input.addEventListener('focus', function () { spots[id].classList.add('is-active'); });
input.addEventListener('blur', function () { spots[id].classList.remove('is-active'); });
});
[no, exp, cvc].forEach(function (i) { i.setCustomValidity('invalid'); });
form.addEventListener('submit', function (e) {
e.preventDefault();
var ok = form.checkValidity();
form.classList.add('was-validated');
document.getElementById('cc-ok').classList.toggle('d-none', !ok);
});
}
function initOrderSummary() {
var apply = document.getElementById('cc-apply');
if (!apply) return;
var promo = document.getElementById('cc-promo');
var msg = document.getElementById('cc-promo-msg');
var subtotal = 57;
function money(n) { return '$' + n.toFixed(2); }
function render(rate) {
var disc = Math.round(subtotal * rate * 100) / 100;
var tax = Math.round((subtotal - disc) * 0.08 * 100) / 100;
var total = subtotal - disc + tax;
document.getElementById('cc-disc-row').classList.toggle('d-none', !rate);
document.getElementById('cc-disc').textContent = '-' + money(disc);
document.getElementById('cc-tax').textContent = money(tax);
document.getElementById('cc-total').textContent = money(total);
document.getElementById('cc-pay').textContent = 'Pay ' + money(total);
}
apply.addEventListener('click', function () {
var ok = promo.value.trim().toUpperCase() === 'SAVE10';
render(ok ? 0.1 : 0);
msg.textContent = ok ? 'Promo applied: 10% off.' : 'That code is not valid.';
msg.className = 'small mb-3 ' + (ok ? 'text-success' : 'text-danger');
});
}
initCheckout();
initOrderSummary();
Built on Bootstrap 5.3.8 with plain JavaScript — no jQuery. Supports light and dark mode via data-bs-theme.