The card expiry date is the date on which a credit or debit card becomes invalid and can no longer be used for transactions. This date is usually printed on the front of the card, along with the cardholder’s name and the card issuer’s logo. The expiry date is typically printed in the format “MM/YY” where MM is the two-digit month and YY is the two-digit year. For example, if the expiry date on a card is “12/23,” it means that the card is valid until the end of December 2023. In this article let’s understand how we can create a regex for Card Expiry Date and how regex can be matched for Card Expiry Date.
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.
Conditions to match a Card Expiry Date
The card expiry date has to be in the format-
- It should have month in MM format
- It should have year in YY or YYYY format
- Month and Year can be optionally separated by /
Regex for checking if age is a Card Expiry Date
Regular Expression-
/^(0[1-9]|1[0-2])\/?([0-9]{4}|[0-9]{2})$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
7283912 | does not match |
12/34322 | does not match |
02/2025 | matches |
10/24 | matches |
109292 | matches |
1024 | matches |
Here is a detailed explanation of the above regex-
/^(0[1-9]|1[0-2])\/?([0-9]{4}|[0-9]{2})$/gm
^ asserts position at start of a line
1st Capturing Group (0[1-9]|1[0-2])
1st Alternative 0[1-9]
0 matches the character 0 with index 4810 (3016 or 608) 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)
2nd Alternative 1[0-2]
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
Match a single character present in the list below [0-2]
0-2 matches a single character in the range between 0 (index 48) and 2 (index 50) (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group ([0-9]{4}|[0-9]{2})
1st Alternative [0-9]{4}
Match a single character present in the list below [0-9]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Alternative [0-9]{2}
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)
$ 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 check the validity of Card Expiry Date. In this article, we explored the significance of a card expiry date for credit or debit cards and discussed its standard format, “MM/YY”. We also delved into the concept of regular expressions (regex) and how they can be employed to validate card expiry dates. The provided regex pattern, /^(0[1-9]|1[0-2])/?([0-9]{4}|[0-9]{2})$/, can effectively match valid card expiry dates. By understanding and implementing this regex, developers can enhance their applications’ user input validation and text manipulation capabilities. Ensure accurate card expiry dates with the power of regex!