Generate fake credit card numbers for testing purposes using Luhn algorithm
These are randomly generated fake credit card numbers. DO NOT use for fraud or illegal activities. These numbers pass Luhn validation but are not real, active cards and will not work for actual transactions.
Credit card generator creates test card numbers that pass Luhn validation. Generate numbers matching Visa, Mastercard, Amex, and other card formats. For software testing, form validation, and development only - not for fraud.
Apply card network prefix (Visa: 4, MC: 51-55, Amex: 34/37), generate random middle digits to reach required length minus one, calculate Luhn check digit, append to create valid number. Optionally generate expiry and CVV.
Card formats: Visa (4xxx, 16 digits), Mastercard (51-55xx or 2221-2720, 16 digits), Amex (34xx/37xx, 15 digits), Discover (6011/65, 16 digits). All use Luhn checksum. CVV: 3 digits (4 for Amex). Expiry: MM/YY.
Test payment forms without real cards.
Verify card input validation logic.
Create realistic payment UI mockups.
Test payment gateway integrations.
const CARD_TYPES = {
visa: { prefix: ['4'], length: 16, cvvLength: 3 },
mastercard: { prefix: ['51', '52', '53', '54', '55'], length: 16, cvvLength: 3 },
amex: { prefix: ['34', '37'], length: 15, cvvLength: 4 },
discover: { prefix: ['6011', '65'], length: 16, cvvLength: 3 }
};
// Luhn check digit
function luhnCheckDigit(number) {
const digits = String(number).split('').map(Number);
digits.push(0);
let sum = 0;
for (let i = digits.length - 1; i >= 0; i--) {
let digit = digits[i];
if ((digits.length - i) % 2 === 0) {
digit *= 2;
if (digit > 9) digit -= 9;
}
sum += digit;
}
return (10 - (sum % 10)) % 10;
}
// Generate card number
function generateCardNumber(type = 'visa') {
const config = CARD_TYPES[type];
if (!config) throw new Error('Invalid card type');
// Random prefix from type's prefixes
const prefix = config.prefix[Math.floor(Math.random() * config.prefix.length)];
// Generate random digits to fill
let number = prefix;
while (number.length < config.length - 1) {
number += Math.floor(Math.random() * 10);
}
// Add check digit
number += luhnCheckDigit(number);
return number;
}
// Generate CVV
function generateCVV(type = 'visa') {
const length = CARD_TYPES[type]?.cvvLength || 3;
return Array(length).fill(0).map(() => Math.floor(Math.random() * 10)).join('');
}
// Generate expiry (future date)
function generateExpiry() {
const now = new Date();
const month = String(Math.floor(Math.random() * 12) + 1).padStart(2, '0');
const year = String(now.getFullYear() + Math.floor(Math.random() * 5) + 1).slice(-2);
return `${month}/${year}`;
}
// Generate complete test card
function generateTestCard(type = 'visa') {
return {
number: generateCardNumber(type).replace(/(\d{4})/g, '$1 ').trim(),
cvv: generateCVV(type),
expiry: generateExpiry(),
type: type.toUpperCase()
};
}
// Examples
console.log(generateTestCard('visa'));
// { number: "4532 1234 5678 9012", cvv: "123", expiry: "06/27", type: "VISA" }
console.log(generateTestCard('amex'));
// { number: "3712 345678 90123", cvv: "1234", expiry: "09/28", type: "AMEX" }Generates test card numbers with proper formats, Luhn validation, CVV, and expiry dates.
Use for testing payment forms, UI development, and validation logic. Always use official test cards from payment processors for integration testing.
No. Generated numbers pass Luhn but are not connected to real accounts. Using them for purchases is fraud and illegal. Use payment processor test cards.