MasterCard credit card number validation

Q

All credit card numbers issued by MasterCard must start with 51 through 55 and have 16 digits. For example, 5271190242643112 is a valid MasterCard credit card number.

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

✍: FYIcenter.com

A

Here is the regular expression for MasterCard credit card number validation:

^5[1-5][0-9]{14}$

^ - begin of the string
5 - matches 5 itself
[1-5] - 1, 2, 3, 4, or 5
[0-9] - any decimal digit
{14} - repeating 14 times
$ - end of the string

Click the button to test this regular expression here online:

2013-01-22, 0👍, 0💬