Created
June 28, 2012 16:58
-
-
Save byrichardpowell/3012503 to your computer and use it in GitHub Desktop.
is Credit Card
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 isCreditCard(CC) { | |
var sum = 0, | |
mul = 1, | |
l = CC.length, | |
i; | |
if ( l > 19 ) { | |
return false; | |
} | |
for ( i = 0; i < l; i++ ) { | |
digit = CC.substring( (l - i) - 1, l - i); | |
tproduct = parseInt(digit, 10) * mul; | |
if (tproduct >= 10) { | |
sum += (tproduct % 10) + 1; | |
} else { | |
sum += tproduct; | |
} | |
if ( mul === 1 ) { | |
mul++; | |
} else { | |
mul--; | |
} | |
} | |
return ( (sum % 10) === 0 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment