1

What would be regular Expression for validating Date format like mm/yyyy. I am new to regular expressions

$.validator.addMethod(
"customDate",
function(value, element) {
    return value.match(?);
},
"Please enter a date in the format mm/yyyy"
);

Can anyone help me in this regard?

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Krishh
  • 4,111
  • 5
  • 42
  • 52
  • With just regex you can easily validate for mm/yyyy format, but it'd consider invalid dates such as 99/9999 as valid without extra checks. – Fabrício Matté May 25 '12 at 00:43
  • 3
    Extract the individual parts (month and year) and then ensure no outlier values, as noted above. **Since the function can contain arbitrary code, there is no reason to limit the solution to a single regular expression.** Instead, write something robust than you can understand [later]. –  May 25 '12 at 00:52

4 Answers4

3

Expanding on RobG's function:

function validateDate(s) {
  return /^([1-9]|1[0-2])\/[12]\d{3}$/.test(s);
}

([1-9]|1[0-2]) checks it is either 1-9 or 10-12. [12]\d{3} makes sure the year is 1 or 2 followed by three numbers (so valid ranges in this case would be 1000-2999; if you need more specific values, please update your question.

EDIT If you want month values of 01-12 instead, change the regex to:

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

David John Welsh
  • 1,564
  • 1
  • 14
  • 23
  • 1
    Perhaps `0?[1-9]|1[0-2]` for the first bit, to allow for the `MM` specified (whether the leading `0` should be optional is up to the OP). – nnnnnn May 25 '12 at 01:01
  • Yeah, originally it was that, but in the comment to RobG's answer, Krishh says "1-12", so I changed it. – David John Welsh May 25 '12 at 01:04
  • Yeah, if the OP doesn't mind if it's `M` or `MM`, add the question mark after the `0` like nnnnnn says. – David John Welsh May 25 '12 at 01:14
  • Thanks a lot for the your answer! You have given a very clear explanation on how the regular expression work. Thanks again.. – Krishh May 25 '12 at 01:19
  • You're more than welcome. Finally I've been able to help someone else instead of just asking questions all the time! Yay! – David John Welsh May 25 '12 at 01:22
2

What are your criteria for "valid"? Is /^\d{2}/\d{4}$/ sufficient or do you need to check that the month is between 1 and 12 inclusive?

Also, it is much more appropriate to use test:

function validateDate(s) {
    return /^\d{2}\/\d{4}$/.test(s);
}

or a more efficient version (only creates the regular expression once):

var validateString = (function() {
    var re = /^\d{2}\/\d{4}$/;
    return function(s) { return re.test(s);}
}());
RobG
  • 142,382
  • 31
  • 172
  • 209
  • I would require it to check for the months between 1 and 12. – Krishh May 25 '12 at 00:49
  • Yes, I'd use `.test()`, too. But this regex allows values that aren't months, like 13, 99 and 00 (see my answer for an example of one that excludes non-valid values) – David John Welsh May 25 '12 at 01:08
0

Try this regular expression:

var regex = /^(\d{2})[\/](\d{4})$/.exec(date);

After that, you could check the regex subgroups for out of bounds ranges like 1000 years from now, etc.

stan
  • 4,885
  • 5
  • 49
  • 72
0

Here ya go:

/^[01]?\d\/\d{4}$/
arychj
  • 711
  • 3
  • 21