reg ex to validate credit card expiry date with months represented by two digits from 01 to 12 and years in two digits from year 23 up to 99 in javascript in javascript

Here's a regular expression in JavaScript to validate a credit card expiry date with months represented by two digits from 01 to 12 and years in two digits from year 23 up to 99:

index.tsx
const expiryDateRegex = /^(0[1-9]|1[0-2])\/(2[3-9]|[3-9][0-9])$/;
66 chars
2 lines

This regular expression consists of two parts:

  • The first part validates the month, which can be 01 to 12. The regular expression uses a group with two alternatives, first 0[1-9] to match months 01-09, and then 1[0-2] to match months 10-12.
  • The second part validates the year, which can be 23 to 99. The regular expression uses a group with two alternatives, first 2[3-9] to match years 23-29, and then [3-9][0-9] to match years 30-99.

Here's an example of how to use this regular expression in JavaScript to validate an expiry date:

index.tsx
const isValidExpiryDate = expiryDateRegex.test('12/24'); // true
const isValidExpiryDate2 = expiryDateRegex.test('13/25'); // false
132 chars
3 lines

gistlibby LogSnag