Luhn Algorithm Validator
How the Luhn algorithm works
- Starting from the rightmost digit, double every second digit.
- If doubling produces a number greater than 9, subtract 9 (equivalent to adding the two digits).
- Sum all digits (doubled and unchanged).
- If the total is divisible by 10, the number is valid.
Example: 79927398713 passes the check β the processed digits sum to 70.
Change any single digit and the sum will no longer be divisible by 10.
Implementation
JavaScript
function isValidLuhn(number) {
const digits = number.replace(/\D/g, '');
let sum = 0;
let double = false;
for (let i = digits.length - 1; i >= 0; i--) {
let digit = Number(digits[i]);
if (double) {
digit *= 2;
if (digit > 9) digit -= 9;
}
sum += digit;
double = !double;
}
return sum % 10 === 0;
}
Python
def is_valid_luhn(number: str) -> bool:
digits = [int(c) for c in number if c.isdigit()]
total = 0
for index, digit in enumerate(reversed(digits)):
if index % 2 == 1:
digit *= 2
if digit > 9:
digit -= 9
total += digit
return total % 10 == 0
Luhn is not the whole story
A number can pass Luhn and still be invalid: the BIN may not exist, the length may be wrong for the network, or the card may simply not be issued. Luhn only detects accidental errors such as typos. For a full check, combine it with the BIN checker, and for practice numbers use the test card generator.
Free payment testing tools
Credit Card Generator
Generate test cards from any BIN
Advanced Generator
Wildcard patterns & bulk output
Test Card Numbers
Official numbers from gateway docs
Luhn Validator
Check if a card number is valid
BIN Checker
Identify the card network from a BIN
Visa Generator
Visa test numbers starting with 4
Mastercard Generator
Mastercard test numbers 51β55 / 2221β2720
Amex Generator
American Express 15-digit test numbers
Discover Generator
Discover test numbers 6011 / 644β649 / 65
Interest Calculator
Estimate credit card interest
Payment Calculator
Plan your monthly payoff
Articles
Payment testing guides & tutorials