Created
March 3, 2017 14:27
-
-
Save SeanJM/645aa7a553b923e35d9e04c936419f28 to your computer and use it in GitHub Desktop.
A function which returns an object based on the credit card number with credit card issuer properties
This file contains hidden or 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 getCreditCardIssuer(number) { | |
number = number.toString(); | |
if (/^4[0-9]{12}(?:[0-9]{3})?$/.test(number)) { // Visa | |
return { | |
issuer : 'Visa', | |
cidLength : 3, | |
length : 16 | |
}; | |
} else if (/^5[1-5][0-9]{14}$/.test(number)) { // MasterCard | |
return { | |
issuer : 'MasterCard', | |
cidLength : 3, | |
length : 16 | |
}; | |
} else if (/^3[47][0-9]{13}$/.test(number)) { // American Express | |
return { | |
issuer : 'American Express', | |
cidLength : 4, | |
length : 15 | |
}; | |
} else if (/^6(?:011|5[0-9]{2})[0-9]{12}$/.test(number)) { // Discover | |
return { | |
issuer : 'Discover', | |
cidLength : 3, | |
length : 16 | |
}; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment