Card Toolkit

Luhn Algorithm · IIN Detection

Validate card numbers against the Luhn algorithm and IIN prefix rules, or generate structurally valid test card numbers for Visa, MasterCard, Amex, and Discover.

Spaces and dashes are ignored automatically.

Enter a card number above to validate it

How card validation works

  1. 1.Strip non-numeric characters and check length (12–19 digits).
  2. 2.Detect the card network from the IIN prefix (first 4–6 digits).
  3. 3.Run Luhn: sum digits right-to-left, doubling every second digit from the right.
  4. 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;
}