Created
January 4, 2012 18:51
-
-
Save deadkarma/1561438 to your computer and use it in GitHub Desktop.
get credit card type
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 GetCreditCardTypeByNumber(ccnumber) { | |
var cc = (ccnumber + '').replace(/\s/g, ''); //remove space | |
if ((/^(34|37)/).test(cc) && cc.length == 15) { | |
return 'AMEX'; //AMEX begins with 34 or 37, and length is 15. | |
} else if ((/^(51|52|53|54|55)/).test(cc) && cc.length == 16) { | |
return 'MasterCard'; //MasterCard beigins with 51-55, and length is 16. | |
} else if ((/^(4)/).test(cc) && (cc.length == 13 || cc.length == 16)) { | |
return 'Visa'; //VISA begins with 4, and length is 13 or 16. | |
} else if ((/^(300|301|302|303|304|305|36|38)/).test(cc) && cc.length == 14) { | |
return 'DinersClub'; //Diners Club begins with 300-305 or 36 or 38, and length is 14. | |
} else if ((/^(2014|2149)/).test(cc) && cc.length == 15) { | |
return 'enRoute'; //enRoute begins with 2014 or 2149, and length is 15. | |
} else if ((/^(6011)/).test(cc) && cc.length == 16) { | |
return 'Discover'; //Discover begins with 6011, and length is 16. | |
} else if ((/^(3)/).test(cc) && cc.length == 16) { | |
return 'JCB'; //JCB begins with 3, and length is 16. | |
} else if ((/^(2131|1800)/).test(cc) && cc.length == 15) { | |
return 'JCB'; //JCB begins with 2131 or 1800, and length is 15. | |
} | |
return '?'; //unknow type | |
} | |
function IsValidCC(str) { //A boolean version | |
if (GetCreditCardTypeByNumber(str) == '?') return false; | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment