Validate Luhn (Credit Card)

Check if a number passes the Luhn algorithm for credit card validation.

Code

Utilities
const digits = num.replace(/\D/g, '').split('').reverse().map(Number);
const sum = digits.reduce((acc, d, i) => {
  if (i % 2 === 1) d *= 2;
  if (d > 9) d -= 9;
  return acc + d;
}, 0);
return sum % 10 === 0;

Parameters

Number string to validate.

Browser·fetch() may be limited by CORS

More JavaScript Snippets