Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Discover credit card

Tags:

regex

I have read through this question, but for Discover card, the starting digits are 6011, 622126-622925, 644-649, 65 instead of just 6011, 65. (Source)

For Discover cards, I picked up this regex from that question ^6(?:011|5[0-9]{2})[0-9]{12}$

I modified it to cover 6011, 644-649& 65 but for 622126-622925, building regex is hard cuz of my poor regex skills.

I have this regex so far 6(?:011|5[0-9]{2}|[4][4-9][0-9]|[2]{2}[1-9])[0-9]{2}$, but it only checks for 622[1-9]**.

How do I modify it so that it accepts only between 622126-622925 for 622*** case?

like image 713
Ashfame Avatar asked Nov 21 '12 19:11

Ashfame


People also ask

How can I get my card number without my card?

Call the credit card company. If you do not have access to your credit card and you can't find your account number on your statement or online, call your credit card company to get your account number. The number for your credit card company should be located on your bill, or you can look online to find it.

How many digits is a Diners Club card?

Diners Club and Carte Blanche cards begin with a 3, followed by a 0, 6, or 8 and have 14 digits.

What is card number in credit card?

A credit card number is a long set of digits usually displayed on the front of the credit card. Its goal is to identify both the credit card issuer and the account holder. It also helps prevent fraud.


1 Answers

Here's your regex (demo):

^6(?:011\d{12}|5\d{14}|4[4-9]\d{13}|22(?:1(?:2[6-9]|[3-9]\d)|[2-8]\d{2}|9(?:[01]\d|2[0-5]))\d{10})$

Needless to say, I won't exactly call this pretty or easy to maintain. I would recommend parsing the number as an integer and using your programming language to do the checks.

You should also use Luhn algorithm to check if the credit card number is valid, and while you could theoretically do this with regex, it would many times worse than this.


Allow me to show you how I arrived at this monstrosity, step by step. First, here is how you match each of those ranges:

6011        # matches 6011
65          # matches 65
64[4-9]     # matches 644-649
622(1(2[6-9]|[3-9]\d)|[2-8]\d{2}|9([01]\d|2[0-5]))  
            # matches 622126-622925

Now, you want to match the rest of the digits:

6011\d{12}        # matches 6011 + 12 digits
65\d{14}          # matches 65 + 14 digits
64[4-9]\d{13}     # matches 644-649 + 13 digits
622(1(2[6-9]|[3-9]\d)|[2-8]\d{2}|9([01]\d|2[0-5]))\d{10}
                  # matches 622126-622925 + 10 digits

Now you can combine all four, and add start and end of line anchors:

^(                  # match start of string and open group
 6011\d{12}|        # matches 6011 + 12 digits
 65\d{14}|          # matches 65 + 14 digits
 64[4-9]\d{13}|     # matches 644-649 + 13 digits
 622(1(2[6-9]|[3-9]\d)|[2-8]\d{2}|9([01]\d|2[0-5]))\d{10}
                    # matches 622126-622925 + 10 digits
)$                  # close group and match end of string

The final product above is a slightly compacted version of the previous regex, and I also made groups non-capturing (that's what those ?: are for).

like image 193
NullUserException Avatar answered Nov 03 '22 19:11

NullUserException