# Regular Expression Solutions to Common Problems

• Print
This chapter is from the book

## Credit Card Numbers

Credit card numbers cannot be truly validated using regular expressions; final validation always requires some interaction with a credit card processing organization. However, regular expression validation can indeed be useful in trapping typos (like one digit too many or too few) before submitting any data anywhere.

All credit cards follow a basic numbering scheme—an opening digit sequence followed by a specified number of digits. We’ll start with MasterCard.

```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```
`5[1-5]\ d{ 14}`
```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```

All MasterCard numbers are 16 digits; the first digit is always 5, and the second digit is 1 through 5. 5[1-5] matches the first two digits; \ d{ 14} matches the next 14 digits.

Visa is a little trickier.

```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```
`4\ d{ 12} (\ d{ 3} )?`
```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```

All Visa numbers start with 4 and are 13 or 16 digits (but not 14 or 15, and so a range cannot be used). 4 matches 4, \ d{ 12} matches the next 12 digits, and (\ d{ 3} )? matches an additional 3 digits if they are present.

American Express requires a much simpler pattern.

```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```
`3[47]\ d{ 13} `
```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```

American Express numbers are 15 digits and start with 34 or 37. 3[47] matches the first 2 digits, and \ d{ 13} matches the remaining 13 digits.

Discover uses a simple pattern.

```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```
`6011\ d{ 14} `
```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```

All Discover cards are 16 digits and start with digits 6011; 6011\ d{ 14} does the trick.

Diners Club is a little trickier.

```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```
`(30[0-5]|36\ d|38\ d)\ d{ 11} `
```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```

Diners Club numbers are 14 digits and begin with 300 through 305, 36, or 38. If the opening digits are 300 through 305, an additional 11 digits are needed, whereas if the opening digits are 36 or 38, an additional 12 digits are needed. To make this simpler, the pattern first matches the first three digits regardless of what they are. (30[0-5]|36\ d|38\ d) has three expressions, any of which must match; 30[0-5] matches 300 through 305, 36\ d matches any three-digit number starting with 36, and 38\ d matches any three-digit number starting with 38. This way, \ d{ 11} can be used to match the remaining 11 digits.

All that remains now is a way to check any of the five card types used here.

```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```
`(5[1-5]\ d{ 14} )|(4\ d{ 12} (\ d{ 3} )?)|(3[47]\ d{ 13} )|(6011\ d{ 14} )|((30[0-5]|36\ d|38\ d)\ d{ 11} )`
```MasterCard: 5212345678901234
Visa 1: 4123456789012
Visa 2: 4123456789012345
Amex: 371234567890123
Discover: 601112345678901234
Diners Club: 38812345678901```

The pattern here uses alternation (providing alternatives or statements) to include all the previous patterns, each separated by a |. The result? Simple validation of all major credit card types.