Created
January 7, 2010 21:40
-
-
Save dokipen/271597 to your computer and use it in GitHub Desktop.
Luhn's on Prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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