Skip to content

Instantly share code, notes, and snippets.

@ondrek
Forked from ShirtlessKirk/luhn.js
Created October 14, 2013 18:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ondrek/6979558 to your computer and use it in GitHub Desktop.
Save ondrek/6979558 to your computer and use it in GitHub Desktop.
// Variant of Avraham Plotnitzky's String.prototype method mixed with the "fast" version
// see: https://sites.google.com/site/abapexamples/javascript/luhn-validation
function luhnChk(luhn) {
var len = luhn.length,
mul = 0,
prodArr = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]],
sum = 0;
while (len--) {
sum += prodArr[mul][parseInt(luhn.charAt(len), 10)];
mul ^= 1;
}
return sum % 10 === 0 && sum > 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment