Created
August 26, 2016 11:23
-
-
Save s-melnikov/966bebe859c82e6cc8b719c0cd2ccf13 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function doLuhnCheck(ccNumber) { | |
| if (/[^0-9-\s]+/.test(ccNumber)) { return false } | |
| var checksum = 0, | |
| digit = 0, | |
| isEven = false | |
| ccNumber = ccNumber.replace(/\D/g, "") | |
| for (var n = ccNumber.length - 1; n >= 0; n--) { | |
| digit = parseInt(ccNumber.charAt(n), 10) | |
| if (isEven) { | |
| if ((digit *= 2) > 9) { | |
| digit -= 9; | |
| } | |
| } | |
| checksum += digit | |
| isEven = !isEven | |
| } | |
| return (checksum % 10) == 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment