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.
I believe you are probably looking for something like this:
Originally posted here @ http://mel-green.com/2008/11/determine-credit-card-type-with-javascript/
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
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