shareef
shareef

Reputation: 9581

Regular Expression Regex for Format MMYY for current year (e.g) 19 and future years in javascript

I need a regular expression to be used in credit card form the rule is simple, the format must be MMYY.

I could achieve that with this regular expression.

/^(0[1-9]|1[0-2])\d{2}$/

Now am researching to apply validation to make YY 19 for the current year and in future years.

Maybe its hard to make it dynamic, but i can replace the string 19 from current year in javascript, so now I just want it fixed for 19 and above.

Example of valid MMYY:

0126

1220

0119

Example of In Valid MMYY

0101

1111

1218

Here is reference of what i have now Example shared for my reg exp looks like

Upvotes: 0

Views: 1606

Answers (4)

Jeff smith
Jeff smith

Reputation: 147

Actually @CertainPerformance is incorrect. It should be: ^(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])$

const validate = (str) => {
  const yearLimit = 19; // or pass this as an argument if you want

  const re = /^(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])$/;
  const match = str.match(re);
  //Match return Array(3) [ "0130", "01", "30" ]
  if (!match) {
    return false;
  }
  const year = Number(match[2]);
  return year >= yearLimit;
};

console.log(
  validate('0121'), //January 2021
  validate('1212'), //December 2012
  validate('1132'), //November 2032
  validate('0136'), //January 2036
);

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 371019

Given a year for which that year and future years should pass, it'd be a bit tedious to dynamically construct such a regular expression. Consider using a capture group instead, and then just check whether the captured YY is greater than or equal to the limit:

const yearLimit = 19; // or pass this as an argument if you want

const re = /^(?:0[1-9]|1[0-2])(\d{2})$/;
const match = str.match(re);
if (!match) {
  return false;
}
const year = Number(match[1]);
return year >= yearLimit;

const validate = (str) => {
  const yearLimit = 19; // or pass this as an argument if you want

  const re = /^(?:0[1-9]|1[0-2])(\d{2})$/;
  const match = str.match(re);
  if (!match) {
    return false;
  }
  const year = Number(match[1]);
  return year >= yearLimit;
};

console.log(
  validate('1234'),
  validate('1212')
);

^(?:0[1-9]|1[0-2])(\d{2})$ means

  • ^ - Match start of string
  • (?:0[1-9]|1[0-2]) - Match 01-12: either
    • 0[1-9] - Leading 0, followed by a number 1 to 9, or
    • 1[0-2] - Leading 1, followed by a number 0 to 2
  • (\d{2}) - Match and capture any two digits
  • $ - Match end of string

Upvotes: 1

Nimish Gupta
Nimish Gupta

Reputation: 3175

Here is the dynamic solution for your answer

    var date = new Date()
    var a = date.getFullYear().toString().substr(2,2).split('')
    var monthRegex = "(0[1-9]|1[0-2])"
    var yearRegex1 = "([" + a[0] + "-9][" + a[1] + "-9])"
    var yearRegex2 = "([" + (parseInt(a[0], 10) + 1) + "-9][0-9])"

    var regex2 = new RegExp("^" + monthRegex + yearRegex1 + "|" + yearRegex2 + "$");
    
    console.log(regex2.test('1218'))
    console.log(regex2.test('1219'))
    console.log(regex2.test('1239'))

Hope this helps

Upvotes: 0

Swaroop Deval
Swaroop Deval

Reputation: 906

Below code will work if you want to solve it by regex only.

const currentYear = 19;
const currentDecade = currentYear/10;
const unitPlaceCurrentYear = currentYear%10;
regex_string = `^(0[1-9]|1[0-2])(1[${unitPlaceCurrentYear}-9]|[${currentDecade+1}-9][0-9])$`

var re = new RegExp(regex_string);

inputs = ['0126','1220','0119','0101','1111','1218'];
inputs.map((str) => {
  let match = str.match(re);
  if (match) console.log('matched: ' + str);
  else console.log('Did not match: ' + str);
});

Upvotes: 0

Related Questions