Created
February 22, 2016 20:26
-
-
Save arempe93/d135655360817f84fcce to your computer and use it in GitHub Desktop.
Javascript credit card validator using Luhn's Algorithm
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
function isValidCreditCard(number) { | |
var len = number.length, | |
multiply = 1, | |
sum = 0, | |
val; | |
while (len--) { | |
val = parseInt(number.charAt(len), 10); | |
sum += (multiply ^= 1) ? Math.trunc(val * 2 / 10) + (val * 2 % 10) : val; | |
} | |
return sum && sum % 10 == 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://jsfiddle.net/ccwz1q4s/
~4.3 million ops/sec - http://jsperf.com/cc-validator