Skip to content

Instantly share code, notes, and snippets.

@you21979
Last active August 29, 2015 14:02
Show Gist options
  • Save you21979/8a29ba65f7ae5e6f56e3 to your computer and use it in GitHub Desktop.
Save you21979/8a29ba65f7ae5e6f56e3 to your computer and use it in GitHub Desktop.
クレカのチェックをとりあえず書いたやつ
var validate = function(ccno){
var len = ccno.length;
if(len !== 16){
return false;
}
var sum = 0;
for(var i = 0; i < len; i++){
var w = parseInt(ccno[i]);
if(i % 2 === 0){
w *= 2;
if(w >= 10){
w -= 9;
}
}
sum += w;
}
return (sum % 10 === 0);
}
var main = function(){
var cards = [
"4452-5113-2004-3046",
"4425 5113 2004 3046",
"4451 5113 2004 3046"
];
cards.forEach(function(no){
no = no.replace(/ /g, '').replace(/-/g, '');
console.log('%s %s', no, validate(no));
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment