Discover credit card number validation

Q

All credit card numbers issued by Diners Club must start with 6011 or 65 and have 16 digits. For example, 6011186767363105 is a valid Visa credit card number.

How to write a single regular expression to validate a Discover credit card number described above?

✍: FYIcenter.com

A

Here is the regular expression to validate a Discover credit card number:

^6(?:011|5[0-9]{2})[0-9]{12}$

^ - begin of the string
6 - matches 6
(?:011|5[0-9]{2}) - non-capturing group of 2 possible parts
011 - matches 011
| - or operator
5[0-9]{2} - 3 digits start with 5
[0-9] - any decimal digit
{12} - repeating 12 times
$ - end of the string

Click the button to test this regular expression here online:

2013-01-22, 2225👍, 0💬