Can credit card type be determined solely from the credit card number?
Is this recommended or should we always ask client for the type of credit card they\'re using?
I have heard one good reason to make them pick (even though you can figure it out). So that they know the list of credit cards you accept.
This implementation in Python should work for AmEx, Discover, Master Card, Visa:
def cardType(number):
number = str(number)
cardtype = "Invalid"
if len(number) == 15:
if number[:2] == "34" or number[:2] == "37":
cardtype = "American Express"
if len(number) == 13:
if number[:1] == "4":
cardtype = "Visa"
if len(number) == 16:
if number[:4] == "6011":
cardtype = "Discover"
if int(number[:2]) >= 51 and int(number[:2]) <= 55:
cardtype = "Master Card"
if number[:1] == "4":
cardtype = "Visa"
return cardtype
If all the credit cards that you accept have the same properties then just let the user enter the card number and other properties (expiry date, CVV, etc). However, some card types require different fields to be entered (e.g. start date or issue number for UK Maestro cards). In those cases, you either have to have all fields, thereby confusing the user, or some Javascript to hide/show the relevant fields, again making the user experience a bit weird (fields disappearing/appearing, as they enter the credit card number). In those cases, I recommend asking for the card type first.