Skip to content

Instantly share code, notes, and snippets.

@peledies
Created December 2, 2014 15:59
Show Gist options
  • Save peledies/f546c0b37bc10f7618fc to your computer and use it in GitHub Desktop.
Save peledies/f546c0b37bc10f7618fc to your computer and use it in GitHub Desktop.
Credit Card Validation
// 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