Created
March 22, 2017 07:34
-
-
Save jatinkrmalik/fd583a72f7857e0bd6414b1fc5995fe6 to your computer and use it in GitHub Desktop.
Code snippet to identify the credit card type from it's number
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 GetCardType(number) | |
{ | |
// visa | |
var re = new RegExp("^4"); | |
if (number.match(re) != null) | |
return "Visa"; | |
// Mastercard | |
re = new RegExp("^5[1-5]"); | |
if (number.match(re) != null) | |
return "Mastercard"; | |
// AMEX | |
re = new RegExp("^3[47]"); | |
if (number.match(re) != null) | |
return "AMEX"; | |
// Discover | |
re = new RegExp("^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)"); | |
if (number.match(re) != null) | |
return "Discover"; | |
// Diners | |
re = new RegExp("^36"); | |
if (number.match(re) != null) | |
return "Diners"; | |
// Diners - Carte Blanche | |
re = new RegExp("^30[0-5]"); | |
if (number.match(re) != null) | |
return "Diners - Carte Blanche"; | |
// JCB | |
re = new RegExp("^35(2[89]|[3-8][0-9])"); | |
if (number.match(re) != null) | |
return "JCB"; | |
// Visa Electron | |
re = new RegExp("^(4026|417500|4508|4844|491(3|7))"); | |
if (number.match(re) != null) | |
return "Visa Electron"; | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment