Ever wondered how apps know your card number is "valid" even before charging you?
Let me tell you a story.
A while ago, I was working on a refund feature in an e-commerce app.
Pretty straightforward:
"Refund ₦5,000 back to the customer’s card."
But something hit me...
❓What if someone tampers with the card number?
❓What if an honest mistake happens and a user misplaces a number in the sequence?
❓How do we know the card number is even real before sending money?
❓How do we save up the transaction charge for invalid transactions?
Sure, payment processors like Stripe or Paystack will yell at you if it's invalid — but how do they know?
Enter: The Luhn Algorithm – a humble, clever math trick that helps catch typos in card numbers before they cause trouble.
Let’s break it down with JavaScript experiments 💻
Step 1: The “Aha!” – Credit Card Numbers Aren’t Just Random
Card numbers aren’t just a bunch of digits; they follow a rule.
If you type them wrong, the app will know instantly — without ever checking with the bank.
So I searched: “how to validate a credit card number?”
Result? Luhn Algorithm.
Fancy name. Sounds like a Bond villain. But it’s just math.
The Luhn algorithm is a checksum formula used to validate identification numbers. It's a simple method designed to detect common errors in numbers like credit card numbers, IMEI numbers, and other identification numbers. The algorithm works by applying basic arithmetic to the digits of a number and calculating a check digit that must agree with the last digit of the original number.
How It Works
It involves doubling every other digit, starting from the rightmost digit, and summing the resulting digits. If any doubled digit is greater than 9, subtract 9. Then, sum all the adjusted digits and the original digits. If the final sum is divisible by 10, the number is considered valid. eg 1762483 sums up to 30, which is divisible by 10, so it is a valid ID number under Luhn's formula.
Step 2: Let's Code the Luhn Check in Baby Steps
- Clean the input: Just digits, no spaces or dashes
function clean(cardNumber) {
return cardNumber.replace(/\D/g, '');
}
- Reverse it – Remember Luhn needs us to count from the right
const reversed = clean(number).split('').reverse();
- Apply the Luhn Magic Every second digit, we double it. If that doubled digit is greater than 9, we subtract 9.
function isValidCard(cardNumber) {
const digits = clean(cardNumber).split('').reverse().map(Number);
const total = digits.reduce((sum, digit, index) => {
if (index % 2 === 1) {
let doubled = digit * 2;
if (doubled > 9) doubled -= 9;
return sum + doubled;
} else {
return sum + digit;
}
}, 0);
return total % 10 === 0;
}
Step 3: Test It Like a Curious Developer
isValidCard('4242 4242 4242 4242'); // true
isValidCard('1234 5678 9012 3456'); // false
isValidCard('1762483'); // true
That’s it! 🎉 You just built a credit card validator using the Luhn algorithm.
🤯 Mind blown? Same.
What I love about this algorithm is:
It's lightweight and doesn't need any API
It’s fast, runs instantly in your app
It’s used everywhere from banking to SIM cards
The next time you build a payment form, form validator, or just want to impress at your next tech meetup — pull this card out (pun intended 😄)
Top comments (0)