Skip to content

Instantly share code, notes, and snippets.

@papeMK2
Last active May 17, 2018 03:13
Show Gist options
  • Save papeMK2/f418c664437a1fa9d04802d6ec5f09d6 to your computer and use it in GitHub Desktop.
Save papeMK2/f418c664437a1fa9d04802d6ec5f09d6 to your computer and use it in GitHub Desktop.
渡されたクレジットカードのブランドを返す
function GetCardBrand(cardNo) {
var map = new Map();
map.set("visa", "^4[0-9]{12}(?:[0-9]{3})?$");
map.set("master", "^5[1-5][0-9]{14}$");
map.set("amex", "^3[47][0-9]{13}$");
map.set("diners", "^3(?:0[0-5]|[68][0-9])[0-9]{11}$");
map.set("jcb", "^(?:2131|1800|35\d{3})\d{11}$");
for (var [key, value] of map) {
var result = cardNo.match(value);
if (result !== null) {
return key;
}
}
return "unknown";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment