Card Toolkit
Luhn Algorithm · IIN DetectionValidate card numbers against the Luhn algorithm and IIN prefix rules, or generate structurally valid test card numbers for Visa, MasterCard, Amex, and Discover. All processing happens in your browser — no card data is transmitted anywhere.
Spaces and dashes are ignored automatically.
Enter a card number above to validate it
How card validation works
- 1.Strip non-numeric characters and check length (12–19 digits).
- 2.Detect the card network from the IIN prefix (first 4–6 digits).
- 3.Run Luhn: sum digits right-to-left, doubling every second digit from the right.
- 4.If total modulo 10 equals 0, the number is structurally valid.
Common issues
- •Luhn-valid ≠ real card — passing the Luhn check means the number is structurally correct, not that it belongs to a real account or has funds.
- •Amex formatting — Amex uses a 4-6-5 grouping (15 digits), not the standard 4-4-4-4. Some APIs require the number without spaces.
- •IIN prefix changes — Mastercard expanded its IIN range to 2-series (222100–272099). Old validators that only check 51–55 will misidentify these.
- •Test vs real prefixes — always use dedicated test card numbers in sandbox environments (e.g. Stripe's 4242 4242 4242 4242), not generated numbers.
Dev snippet — Luhn check (JavaScript)
function luhn(cardNumber) {
const digits = cardNumber.replace(/\D/g, "").split("").map(Number);
let sum = 0;
for (let i = digits.length - 1; i >= 0; i--) {
let d = digits[i];
if ((digits.length - 1 - i) % 2 === 1) {
d *= 2;
if (d > 9) d -= 9;
}
sum += d;
}
return sum % 10 === 0;
}Frequently asked questions
What is the Luhn algorithm?
The Luhn algorithm (ISO/IEC 7812-1) is a checksum formula that validates card numbers. It works by doubling alternating digits from the right, summing the results — a valid number produces a total divisible by 10.
Are the generated test card numbers real?
No. Generated numbers pass the Luhn check and use correct IIN prefixes, but do not correspond to any real account. They are suitable for testing payment form validation but will be declined by any real payment processor.
What is an IIN/BIN prefix?
The Issuer Identification Number (IIN), also called BIN, is the first 4–6 digits of a card number. It identifies the card network and issuer: Visa starts with 4; Mastercard with 51–55 or 2221–2720; Amex with 34 or 37.
Why is my Mastercard being identified as unknown?
Mastercard expanded its IIN range to include 2221–2720. This tool handles both the classic 51–55 range and the newer 2-series cards correctly.