Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What pattern should be used to validate 2 BIN MasterCard numbers [closed]

"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}$/;
like image 257
dml Avatar asked May 20 '16 17:05

dml


1 Answers

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

like image 179
crackmigg Avatar answered Nov 09 '22 01:11

crackmigg