Skip to content

Instantly share code, notes, and snippets.

@agodin3z
Last active November 17, 2020 01:58
Show Gist options
  • Save agodin3z/eaf351b41fe3e30df4f14afb45beafae to your computer and use it in GitHub Desktop.
Save agodin3z/eaf351b41fe3e30df4f14afb45beafae to your computer and use it in GitHub Desktop.
Get credit/debit card type
export const getCardType = (num) => {
let type = 'unknow';
if (num.match('^3[47]\\d{0,13}')) {
type = 'amex';
} else if (num.match('^(?:6011|65\\d{0,2}|64[4-9]\\d?)\\d{0,12}')) {
type = 'discover';
} else if (num.match('^3(?:0([0-5]|9)|[689]\\d?)\\d{0,11}')) {
type = 'diners';
} else if (num.match('^(5[1-5]\\d{0,2}|22[2-9]\\d{0,1}|2[3-7]\\d{0,2})\\d{0,12}')) {
type = 'mastercard';
} else if (num.match('^(?:2131|1800)\\d{0,11}') || num.match('^(?:35\\d{0,2})\\d{0,12}')) {
type = 'jcb';
} else if (num.match('^(?:5[0678]\\d{0,2}|6304|67\\d{0,2})\\d{0,12}')) {
type = 'maestro';
} else if (num.match('^4\\d{0,15}')) {
type = 'visa';
} else if (num.match('^62\\d{0,14}')) {
type = 'unionpay';
} else {
type = 'unknow';
}
return type;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment