DebugPointer
Published on

Regex for Debit Card validation

Regex for Debit Card validation

Most common debit cards are either VISA or Mastercard. Visa facilitates electronic funds transfers throughout the world, most commonly through Visa-branded credit cards, debit cards and prepaid cards. They don't issue cards, but, they provide financial institutions ability to launch Visa branded cards. MasterCard facilitates electronic funds transfers throughout the world, most commonly through MasterCard-branded credit cards, debit cards and prepaid cards. They don't issue cards, but, they provide financial institutions ability to launch MasterCard branded cards. In this article let's understand how we can create a regex for Debit Card and how regex can be matched for Debit Card.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of Debit Card

  • It should be 13 or 16 digits long, new cards have 16 digits and old cards have 13 digits.
  • It should not contain any alphabet or special characters.
  • It should start with 4.
  • If the cards have 13 digits the next 12 digits should be any number between 0-9.
  • If the cards have 16 digits the next 15 digits should be any number between 0-9.

Regex for checking if Debit Card is valid or not

Regular Expression contains VISA and Mastercard regex-

The first part is Mastercard regex, we have an OR operator and then we have VISA regex.

/^((?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12})|(4[0-9]{12}(?:[0-9]{3})?)$/gm

Test string examples for the above regex-

Input StringMatch Output
2233172283924does not match
5503245678654335matches - MasterCard
4333121212does not match
4912001201020matches - VISA

Here is a detailed explanation of the above regex-

/^((?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12})|(4[0-9]{12}(?:[0-9]{3})?)$/gm

1st Alternative ^((?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12})
^ asserts position at start of a line
1st Capturing Group ((?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12})
Non-capturing group (?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)
1st Alternative 5[1-5][0-9]{2}
5 matches the character 5 with index 5310 (3516 or 658) literally (case sensitive)
Match a single character present in the list below [1-5]
1-5 matches a single character in the range between 1 (index 49) and 5 (index 53) (case sensitive)
Match a single character present in the list below [0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Alternative 222[1-9]
222 matches the characters 222 literally (case sensitive)
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case sensitive)
3rd Alternative 22[3-9][0-9]
22 matches the characters 22 literally (case sensitive)
Match a single character present in the list below [3-9]
3-9 matches a single character in the range between 3 (index 51) and 9 (index 57) (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
4th Alternative 2[3-6][0-9]{2}
2 matches the character 2 with index 5010 (3216 or 628) literally (case sensitive)
Match a single character present in the list below [3-6]
3-6 matches a single character in the range between 3 (index 51) and 6 (index 54) (case sensitive)
Match a single character present in the list below [0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
5th Alternative 27[01][0-9]
27 matches the characters 27 literally (case sensitive)
Match a single character present in the list below [01]
01 matches a single character in the list 01 (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
6th Alternative 2720
2720 matches the characters 2720 literally (case sensitive)
Match a single character present in the list below [0-9]
{12} matches the previous token exactly 12 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Alternative (4[0-9]{12}(?:[0-9]{3})?)$
2nd Capturing Group (4[0-9]{12}(?:[0-9]{3})?)
4 matches the character 4 with index 5210 (3416 or 648) literally (case sensitive)
Match a single character present in the list below [0-9]
{12} matches the previous token exactly 12 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
Non-capturing group (?:[0-9]{3})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [0-9]
{3} matches the previous token exactly 3 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to match Debit Card number regex pattern.