Skip to content

Instantly share code, notes, and snippets.

@alanleite
Created August 15, 2018 21:59
Show Gist options
  • Save alanleite/78ab7b52ff35a85c9c2ded4e1302d7fe to your computer and use it in GitHub Desktop.
Save alanleite/78ab7b52ff35a85c9c2ded4e1302d7fe to your computer and use it in GitHub Desktop.
Card Brand by Number
module.exports = function(number) {
const rules = [
{
type: 'elo',
pattern: /^(((40117[89])|(431274)|(438935)|(451416)|(457393)|(45763[12])|(504175)|(627780)|(636297)|(636368)|(65500[0-3])|(65165[2-4])|(65048[5-8])|(650489|65049[0-4])|(506699|5067[0-6][0-9]|50677[0-8])|(509[0-8][0-9]{2}|5099[0-8][0-9]|50999[0-9])|(65003[1-3])|(65003[5-9]|65004[0-9]|65005[01])|(65040[5-9]|6504[1-3][0-9])|(65048[5-9]|65049[0-9]|6505[0-2][0-9]|65053[0-8])|(65054[1-9]|6505[5-8][0-9]|65059[0-8])|(65070[0-9]|65071[0-8])|(65072[0-7])|(65090[1-9]|65091[0-9]|650920)|(65165[2-9]|6516[67][0-9])|(65500[0-9]|65501[0-9])|(65502[1-9]|6550[34][0-9]|65505[0-8]))\d{0,16})$/,
length: [16],
cvcLength: [3]
}, {
type: 'hipercard',
pattern: /^(606282\d{10}(\d{3})?)|(3841\d{15})$/,
format: /\d/g,
length: [13, 16, 19],
cvcLength: [3]
}, {
type: 'aura',
pattern: /^(50)/,
length: [16],
cvcLength: [3]
}, {
type: 'visaelectron',
pattern: /^4(026|17500|405|508|844|91[37])/,
length: [16],
cvcLength: [3]
}, {
type: 'maestro',
pattern: /^(5(018|0[23]|[68])|6(39|7))/,
length: [12, 13, 14, 15, 16, 17, 18, 19],
cvcLength: [3]
}, {
type: 'forbrugsforeningen',
pattern: /^600/,
length: [16],
cvcLength: [3]
}, {
type: 'dankort',
pattern: /^5019/,
length: [16],
cvcLength: [3]
}, {
type: 'visa',
pattern: /^4/,
length: [13, 16],
cvcLength: [3]
}, {
alias: 'master',
type: 'mastercard',
pattern: /(?:(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12})/,
length: [16],
cvcLength: [3]
}, {
type: 'amex',
pattern: /^3[47]/,
length: [15],
cvcLength: [3, 4]
}, {
type: 'dinersclub',
pattern: /^(?:3(?:0[0-5]|[68][0-9])[0-9]{11})$/,
length: [14],
cvcLength: [3]
}, {
type: 'discover',
pattern: /^6([045]|22)/,
length: [16],
cvcLength: [3]
}, {
type: 'unionpay',
pattern: /^(62|88)/,
length: [16, 17, 18, 19],
cvcLength: [3]
}, {
type: 'jcb',
pattern: /^35/,
length: [16],
cvcLength: [3]
}
];
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
const result = rule.pattern.test(number)
if(result) return rule.type
}
return null
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment