Last updated
Validating a Credit Card Number
Enter a credit card number to check if it passes the Luhn algorithm. Example with a Visa test number:
Input: 4532015112830366
Step-by-step Luhn validation:
─────────────────────────────────────────────────────────────────────
Original digits: 4 5 3 2 0 1 5 1 1 2 8 3 0 3 6 6
Step 1: Double every second digit from the right (positions 2,4,6...):
Position: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Digit: 4 5 3 2 0 1 5 1 1 2 8 3 0 3 6 6
Double: 4 10 3 4 0 2 5 2 1 4 8 6 0 6 6 6
Step 2: Sum digits of doubled values > 9 (10 → 1+0=1):
4 1 3 4 0 2 5 2 1 4 8 6 0 6 6 6
Step 3: Sum all digits:
4+1+3+4+0+2+5+2+1+4+8+6+0+6+6+6 = 58... wait
Corrected sum: 4+1+3+4+0+2+5+2+1+4+8+6+0+6+6+6 = 58
Step 4: Check if sum mod 10 = 0:
58 mod 10 = 8 ≠ 0
Result: ✗ INVALID (this is a demonstration — use real test numbers)
Validating a Known Valid Test Card
Standard test card numbers used in payment development always pass Luhn:
Input: 4111111111111111 (Visa test card)
Luhn validation:
Digits: 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Double alternating (from right):
8 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1
Sum digits > 9: (8 stays 8, all others are single digits)
8 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1
Sum: 8+1+2+1+2+1+2+1+2+1+2+1+2+1+2+1 = 30
30 mod 10 = 0 ✓
Result: ✓ VALID — passes Luhn check
Card type: Visa (starts with 4, 16 digits)
Common Test Card Numbers
Standard test card numbers that pass Luhn validation for development use:
Visa:
4111111111111111 ✓ Valid
4242424242424242 ✓ Valid
4000056655665556 ✓ Valid
Mastercard:
5555555555554444 ✓ Valid
5105105105105100 ✓ Valid
2223003122003222 ✓ Valid
American Express:
378282246310005 ✓ Valid (15 digits)
371449635398431 ✓ Valid (15 digits)
Discover:
6011111111111117 ✓ Valid
6011000990139424 ✓ Valid
Note: These numbers pass Luhn validation but are not
assigned to real accounts. They will be declined by
payment processors in live mode.
Detecting a Typo in a Card Number
The Luhn algorithm catches single-digit errors. A user types one digit wrong:
Correct number: 4111111111111111 ✓ Valid
Typo (last digit changed): 4111111111111112
Luhn check on 4111111111111112:
Sum: 31
31 mod 10 = 1 ≠ 0
Result: ✗ INVALID
The Luhn algorithm detected the single-digit typo.
Show the user an error before submitting to the payment processor.
Validating an IMEI Number
Mobile device IMEI numbers also use the Luhn algorithm:
Input: 490154203237518 (15-digit IMEI)
Luhn validation:
Digits: 4 9 0 1 5 4 2 0 3 2 3 7 5 1 8
Double alternating from right:
4 18 0 2 5 8 2 0 3 4 3 14 5 2 8
Sum digits > 9 (18→9, 14→5):
4 9 0 2 5 8 2 0 3 4 3 5 5 2 8
Sum: 4+9+0+2+5+8+2+0+3+4+3+5+5+2+8 = 60
60 mod 10 = 0 ✓
Result: ✓ VALID IMEI
Generating a Valid Test Number
Generate a valid Luhn number for a specific card prefix:
Settings:
Prefix: 4532 (Visa)
Total length: 16 digits
Generated number: 4532015112830366
Verification:
The last digit (6) is the check digit, calculated to make
the entire number pass the Luhn algorithm.
✓ Passes Luhn validation
✓ Correct length for Visa (16 digits)
✓ Correct prefix for Visa (starts with 4)
JavaScript Implementation
The checker provides implementation code you can use in your own applications:
// JavaScript Luhn validation
function luhnCheck(cardNumber) {
const digits = cardNumber.replace(/\D/g, '').split('').map(Number);
let sum = 0;
let isEven = false;
for (let i = digits.length - 1; i >= 0; i--) {
let digit = digits[i];
if (isEven) {
digit *= 2;
if (digit > 9) digit -= 9;
}
sum += digit;
isEven = !isEven;
}
return sum % 10 === 0;
}
// Usage
luhnCheck('4111111111111111'); // true
luhnCheck('4111111111111112'); // false
Python Implementation
def luhn_check(card_number: str) -> bool:
digits = [int(d) for d in card_number if d.isdigit()]
total = 0
for i, digit in enumerate(reversed(digits)):
if i % 2 == 1: # every second digit from right
digit *= 2
if digit > 9:
digit -= 9
total += digit
return total % 10 == 0
# Usage
print(luhn_check('4111111111111111')) # True
print(luhn_check('1234567890123456')) # False
Card Type Detection by Prefix
The checker also identifies the card type from the number prefix:
Card Type Detection Rules:
─────────────────────────────────────────────────────────────────────
Visa: Starts with 4, length 13 or 16
Mastercard: Starts with 51-55 or 2221-2720, length 16
American Express: Starts with 34 or 37, length 15
Discover: Starts with 6011, 622126-622925, 644-649, or 65, length 16
JCB: Starts with 3528-3589, length 16
Diners Club: Starts with 300-305, 36, or 38, length 14
Input: 5555555555554444
Prefix 55 → Mastercard
Length 16 → ✓ Correct for Mastercard
Luhn check → ✓ Valid
Result: Valid Mastercard number