i have an array of card types that looks something like this
var cards = new Array();
cards [0] = {name: "VISA", length: "13,16", prefixes: "4", checkdigit: true};
cards [1] = {name: "VISA_DELTA/ELECTRON", length: "16", prefixes: "417500,4917,4913", checkdigit: true};
however id like to be able to find the card type depending on the card number entered. e.g if they select their card type from a drop down list i.e visa, then the credit card number should start with 4 otherwise when they submit the form a message of some sort should be display saying this is (whatever card it is, please change card type). Any help would be appreciated.
the id for the card number text field is CardNumber. Im not sure what other info may be needed, i have a function called Validate which validates the rest of the form and a function call Calculate which does the luhn check.
Identify a bank card's credit company by looking at the first number. Cards that start with a "3" are American Express. Those that start with "4" are Visa credit and debit cards, those that start with "5" are MasterCard credit and debit cards, and those that start with "6" are Discover credit cards.
Bottom line Curious how to know if a card is credit or debit? They look quite similar, but debit cards will inform you on either the front or back. You can also type in your debit or credit card number on the Binlist website. It'll identify the nature of your card when you enter the first six digits of the card number.
You can tell if your card is a debit card by looking at the right-hand side of the card where it will say “Debit” on either the top or the bottom corner. A credit card is a bank card that enables you to make purchases now and pay for them later.
Visa starts with 4, a Mastercard is 5 and Discover is 6. Other numbers are used to identify the industry. For instance, 1 and 2 are used for the airline industry. The numeral 3 represents travel and entertainment, so it makes sense that 3 also indicates it's an American Express card.
I believe you are probably looking for something like this:
<script type="text/javascript">
function GetCardType(number)
{
// visa
var re = new RegExp("^4");
if (number.match(re) != null)
return "Visa";
// Mastercard
// Updated for Mastercard 2017 BINs expansion
if (/^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$/.test(number))
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 "";
}
</script>
Originally posted here @ http://mel-green.com/2008/11/determine-credit-card-type-with-javascript/
Here's a jQuery plugin designed for exactly this:
http://jquerycreditcardvalidator.com/
See also this StackOverflow answer for an excellent discussion of how credit card numbers are related to card types:
https://stackoverflow.com/a/72801/12484
The card companies have a fairly well-defined list of prefixes which are specific to the card type.
These prefixes range from a single digit (everything begining with '5' is Mastercard) to some strings up to six or seven digits long, for more obscure card types, and also for mainstream cards, but being able to identify not only the card type but also the issuing bank.
Here's one resource I found: http://www.beachnet.com/~hstiles/cardtype.html
You may also want to see Wikipedia: http://en.wikipedia.org/wiki/Credit_card_numbers
The down side is that while the prefixes that are in circulation now are pretty much fixed, there's always the likelyhood that they'll need to come up with more, so you would need to ensure that you keep any prefix list up-to-date, especially if you're using it to check the longer prefix ranges.
The card prefix scheme is detailed in BIN (bank id number) lists and change regularly, I would advise against validating against the full 1st 6 digits of the PAN unless you plan regular updates and only perform a cursory check on the first couple of digits (your missing 48* for for visa/electon for example and visa length can span 16-19).
If your in the UK; http://www.barclaycard.co.uk/business/documents/pdfs/bin_rules.pdf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With