This article is from the Credit cards and Consumer Credit FAQ, by firstname.lastname@example.org (Steve Adams) with numerous contributions by others.
ANSI Standard X4.13-1983 answers this question for most national
systems. (Phone, gas, and department-store cards have their own
numbering schemes.) I have not read ANSI X4.13, but a correspondent
was kind enough to provide excerpts, which follow.
The first digit is the system: 3=T&E cards, 4=Visa, 5=MasterCard,
6=Discover. The structure of the card number varies by system:
- American Express starts with 37; Carte Blanche and Diners Club
with 38. For Amex, digits 3-4 are type and currency, digits 5-11
are account number, digits 12-14 are card number within account,
and digit 15 is a check digit.
- Visa: digits 2-6 are the bank number; digits 7-12 or 7-15 are the
account number, and digit 13 or 16 is a check digit.
- MasterCard: digits 2-3, 2-4, 2-5, or 2-6 are the bank number,
depending on whether digit 2 is a 1, 2, 3, or something else. The
digits after the bank number up through digit 15 are the account
number, and digit 16 is a check digit.