Credit Card Validator
Security & Authentication Tool
Supported Card Types
- Visa: Starts with 4, 13-16 digits
- Mastercard: Starts with 51-55 or 2-27, 16 digits
- American Express: Starts with 34 or 37, 15 digits
- Discover: Starts with 6011 or 65, 16 digits
- JCB, Diners Club, UnionPay, Maestro
No data is sent anywhere — all validation happens locally in your browser.
Copied to clipboard!
lucide.createIcons();
const input = document.getElementById('cc-input');
function luhnCheck(num) {
let sum = 0, alt = false;
for (let i = num.length - 1; i >= 0; i--) {
let d = parseInt(num[i], 10);
if (alt) { d *= 2; if (d > 9) d -= 9; }
sum += d; alt = !alt;
}
return sum % 10 === 0;
}
function detectCardType(num) {
const patterns = [
{ name: 'Visa', pattern: /^4/, lengths: [13, 16, 19] },
{ name: 'Mastercard', pattern: /^5[1-5]/, lengths: [16] },
{ name: 'Mastercard 2', pattern: /^2[2-7]/, lengths: [16] },
{ name: 'Amex', pattern: /^3[47]/, lengths: [15] },
{ name: 'Discover', pattern: /^6(?:011|5)/, lengths: [16] },
{ name: 'Diners Club', pattern: /^3(?:0[0-5]|[68])/, lengths: [14, 16, 19] },
{ name: 'JCB', pattern: /^35(?:2[89]|[3-8])/, lengths: [16, 19] },
{ name: 'UnionPay', pattern: /^62/, lengths: [16, 17, 18, 19] },
{ name: 'Maestro', pattern: /^(?:5[06-8]|6\d)/, lengths: [12, 13, 14, 15, 16, 17, 18, 19] }
];
return patterns.find(p => p.pattern.test(num) && p.lengths.includes(num.length));
}
function update() {
const raw = input.value.replace(/\D/g, '');
const result = document.getElementById('cc-result');
if (!raw) { result.innerHTML = ''; return; }
const valid = luhnCheck(raw);
const type = detectCardType(raw);
const formatted = raw.replace(/(.{4})/g, '$1 ').trim();
document.getElementById('cc-formatted').textContent = formatted;
let html = ``;
html += valid ? '✔ Valid' : '✘ Invalid';
html += `
`;
if (type) html += `Type: ${type.name}
`;
html += `Length: ${raw.length} digits
`;
if (!valid && raw.length > 0) {
const suggestions = [];
if (raw.length < 13) suggestions.push('Number too short');
if (raw.length > 19) suggestions.push('Number too long');
suggestions.push('May contain a typo (failed Luhn check)');
html += `${suggestions.join(' • ')}
`;
}
result.innerHTML = html;
}
input.addEventListener('input', update);
function showToast(msg) {
const toast = document.getElementById('toast');
const toastMsg = document.getElementById('toast-message');
toastMsg.textContent = msg;
toast.classList.add('show');
setTimeout(() => toast.classList.remove('show'), 2000);
}