Last active
March 8, 2020 14:27
-
-
Save stefano-bortolotti/7224025 to your computer and use it in GitHub Desktop.
Get credit card type [JS]
This file contains 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
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