9

"2" series BIN MasterCard numbers will start in October 2016. What regex pattern should be used to validate them. Today, we use the below pattern for MasterCards which start with 5:

var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
dml
  • 132
  • 1
  • 4

1 Answers1

16

The answer by @Rawing incorrectly assumes that the BIN range of a MasterCard number will be changed to the new range while it is correct that the BIN range will be extended by the new range.

Therefore for future visitors that (blindly) copy the regex you should use this version to allow all "valid" MasterCard numbers (excluding luhn-check):

/^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$|^2(?:2(?:2[1-9]|[3-9]\d)|[3-6]\d\d|7(?:[01]\d|20))-?\d{4}-?\d{4}-?\d{4}$/

Or this version without allowing dashes between the numbers:

/^5[1-5]\d{14}$|^2(?:2(?:2[1-9]|[3-9]\d)|[3-6]\d\d|7(?:[01]\d|20))\d{12}$/

This is basically a combination of @Rawings answer and the question.

I know this does not strictly answer the question but will hopefully prevent some copy-paste bugs in payment forms.

Extended Demo

crackmigg
  • 5,571
  • 2
  • 30
  • 40
  • That was my first thought when I didn't see a "5" in the original answer. Thanks! – Dss Aug 17 '16 at 16:33
  • @migg : working (Y) Can you please provide sample mastercard numbers for the said range i.e 2221-2720, I need it for testing? – Rahul Hendawe Oct 06 '16 at 13:46
  • The numbers in the extended demo match the regex but fail the checksum validation needed for credit card numbers. Link to a demo with valid credit card numbers: https://regex101.com/r/cGQNpd/3 – Sarah Elan Nov 10 '16 at 16:43
  • And if anyone wants to handle randomly inserted spaces or dashes as it often happens in real production, use this: `(([5][\s-]*)([1-7][\s-]*)(\d[\s-]*){14})|2[\s-]*(?:2[\s-]*(?:2[\s-]*[1-9]|[3-9][\s-]*\d)|[3-6][\s-]*\d[\s-]*\d|7[\s-]*(?:[01][\s-]*\d|2[\s-]*0[\s-]*))(\d[\s-]*){12}` – ajeh Jun 06 '17 at 15:40
  • 1
    Sarah Elan, you are refferring to the Luhn algorithm? – Louis Duran Jun 14 '17 at 20:48