- これを見たので試してみたくなった http://cards.hateblo.jp/entry/card-number-kakunin/
-
-
Save you21979/8a29ba65f7ae5e6f56e3 to your computer and use it in GitHub Desktop.
クレカのチェックをとりあえず書いたやつ
This file contains hidden or 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
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