Skip to content

Instantly share code, notes, and snippets.

@miped
Last active December 11, 2015 09:09
Show Gist options
  • Save miped/4578315 to your computer and use it in GitHub Desktop.
Save miped/4578315 to your computer and use it in GitHub Desktop.
Test for major credit card types
function detect_card_type(card_number) {
if (!_.isString(card_number) || _.isEmpty(card_number.trim())) {
return null;
}
c = card_number.trim();
// http://en.wikipedia.org/wiki/List_of_Issuer_Identification_Numbers
if (/^[1-2]/.test(c)) return 'airline';
if (/^3[06]/.test(c)) return 'diners';
if (/^3[47]/.test(c)) return 'amex';
if (/^35/.test(c)) return 'jcb';
if (/^4/.test(c)) return 'visa';
if (/^5[1-5]/.test(c)) return 'mastercard';
return 'other';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment