Created
December 2, 2014 15:59
-
-
Save peledies/f546c0b37bc10f7618fc to your computer and use it in GitHub Desktop.
Credit Card Validation
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
// thanks to http://datagenetics.com/blog/july42013/index.html | |
function mod10_check(val){ | |
var nondigits = new RegExp(/[^0-9]+/g); | |
var number = val.replace(nondigits,''); | |
var pos, digit, i, sub_total, sum = 0; | |
var strlen = number.length; | |
if(strlen < 13){ return false; } | |
for(i=0;i<strlen;i++){ | |
pos = strlen - i; | |
digit = parseInt(number.substring(pos - 1, pos)); | |
if(i % 2 == 1){ | |
sub_total = digit * 2; | |
if(sub_total > 9){ | |
sub_total = 1 + (sub_total - 10); | |
} | |
} else { | |
sub_total = digit; | |
} | |
sum += sub_total; | |
} | |
if(!sum){ return false; } | |
if(sum % 10 == 0){ | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment