Skip to content

Instantly share code, notes, and snippets.

@stefano-bortolotti
Last active March 8, 2020 14:27
Show Gist options
  • Save stefano-bortolotti/7224025 to your computer and use it in GitHub Desktop.
Save stefano-bortolotti/7224025 to your computer and use it in GitHub Desktop.
Get credit card type [JS]
function getCreditCardType(creditCardNumber) {
// start without knowing the credit card type
var result = "unknown";
// first check for MasterCard
if (/^5[1-5]/.test(creditCardNumber)) {
result = "mastercard";
}
// then check for Visa
else if (/^4/.test(creditCardNumber)) {
result = "visa";
}
// then check for AmEx
else if (/^3[47]/.test(creditCardNumber)) {
result = "amex";
}
// then check for Diners
else if (/3(?:0[0-5]|[68][0-9])[0-9]{11}/.test(creditCardNumber)) {
result = "diners"
}
// then check for Discover
else if (/6(?:011|5[0-9]{2})[0-9]{12}/.test(creditCardNumber)) {
result = "discover";
}
// then check for Maestro
else if (/^(5[06-8]|6\d)\d{14}(\d{2,3})?$/.test(creditCardNumber)) {
result = "maestro";
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment