Validate a credit card number using the Luhn algorithm.
Code
Generalconst digits = cardNumber.replace(/\D/g, '').split('').map(Number);
const sum = digits.reverse().reduce((acc, d, i) => {
if (i % 2) d *= 2;
return acc + (d > 9 ? d - 9 : d);
}, 0);
return sum % 10 === 0;Parameters
The credit card number to validate
Browser·fetch() may be limited by CORS
More JavaScript Snippets
Check Balanced Brackets
Check if brackets in a string are properly balanced and nested using a stack-based approach.
Check Password Strength
Check if a password meets strength requirements (8+ chars, uppercase, lowercase, number, special).
Get Type
Get the precise type of any value.
Is Alpha Only
Check if a string contains only alphabetic characters.
Is Alphanumeric
Check if a string contains only letters and numbers.
Is Array
Check if a value is an array.