Skip to content

Instantly share code, notes, and snippets.

@dokipen
Created January 7, 2010 21:40
Show Gist options
  • Save dokipen/271597 to your computer and use it in GitHub Desktop.
Save dokipen/271597 to your computer and use it in GitHub Desktop.
Luhn's on Prototype
/**
* Luhn's algorithm in javascript with prototype.
*
* This is used for credit card number validation.
*
* You'll have to scrub the number before passing it in. If any
* non-numeric characters are passed then this will fail.
*
* @see http://en.wikipedia.org/wiki/Luhn_algorithm
*/
luhn = function(cc) {
return $A(cc).reverse().map(Number).inject(0, function(s, d, i) {
return s + (i % 2 == 1 ? (d == 9 ? 9 : (d * 2) % 9) : d);
}) % 10 == 0;
};
@dokipen
Copy link
Author

dokipen commented Apr 12, 2010

Luhn's algorithm in javascript with prototype.

This is used for credit card number validation.

You'll have to scrub the number before passing it in. If any
non-numeric characters are passed then this will fail.

@see http://en.wikipedia.org/wiki/Luhn_algorithm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment