Reputation: 101
I've been searching high and low for a regex that will validate MM/DD only. I do not need year (we're asking in a form for fiscal year ends, so we don't need a year, we need to know the range of their fiscal year).
For example, I need them to be able to enter 06/30 or 12/31 for Jun 30th or Dec 31st, respectively. All the regex examples I was able to find include the year as part of the date, which I do not need and I am very unfamiliar with regular expression syntax. What regex can I use for for two digit month and day only?
Upvotes: 4
Views: 15225
Reputation: 13006
The pattern you need is:
#(0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01])|(0[469]|11)/(0[1-9]|[12][0-9]|30)|02/(0[1-9]|[12][0-9])#
Testing it in PHP:
<?
$p = '#' . implode('|', array(
'(0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01])', // 31-day months
'(0[469]|11)/(0[1-9]|[12][0-9]|30)', // 30-day months
'02/(0[1-9]|[12][0-9])', // February
)) . '#';
for ($m = 0; $m < 14; $m++) {
for ($d = 0; $d < 33; $d++) {
$date = sprintf('%02d/%02d', $m, $d);
echo $date . ' - ' . preg_match($p, $date) . "\n";
}
echo "\n";
}
And the results are:
00/00 - 0
00/01 - 0
00/02 - 0
00/03 - 0
00/04 - 0
00/05 - 0
00/06 - 0
00/07 - 0
00/08 - 0
00/09 - 0
00/10 - 0
00/11 - 0
00/12 - 0
00/13 - 0
00/14 - 0
00/15 - 0
00/16 - 0
00/17 - 0
00/18 - 0
00/19 - 0
00/20 - 0
00/21 - 0
00/22 - 0
00/23 - 0
00/24 - 0
00/25 - 0
00/26 - 0
00/27 - 0
00/28 - 0
00/29 - 0
00/30 - 0
00/31 - 0
00/32 - 0
01/00 - 0
01/01 - 1
01/02 - 1
01/03 - 1
01/04 - 1
01/05 - 1
01/06 - 1
01/07 - 1
01/08 - 1
01/09 - 1
01/10 - 1
01/11 - 1
01/12 - 1
01/13 - 1
01/14 - 1
01/15 - 1
01/16 - 1
01/17 - 1
01/18 - 1
01/19 - 1
01/20 - 1
01/21 - 1
01/22 - 1
01/23 - 1
01/24 - 1
01/25 - 1
01/26 - 1
01/27 - 1
01/28 - 1
01/29 - 1
01/30 - 1
01/31 - 1
01/32 - 0
02/00 - 0
02/01 - 1
02/02 - 1
02/03 - 1
02/04 - 1
02/05 - 1
02/06 - 1
02/07 - 1
02/08 - 1
02/09 - 1
02/10 - 1
02/11 - 1
02/12 - 1
02/13 - 1
02/14 - 1
02/15 - 1
02/16 - 1
02/17 - 1
02/18 - 1
02/19 - 1
02/20 - 1
02/21 - 1
02/22 - 1
02/23 - 1
02/24 - 1
02/25 - 1
02/26 - 1
02/27 - 1
02/28 - 1
02/29 - 1
02/30 - 0
02/31 - 0
02/32 - 0
03/00 - 0
03/01 - 1
03/02 - 1
03/03 - 1
03/04 - 1
03/05 - 1
03/06 - 1
03/07 - 1
03/08 - 1
03/09 - 1
03/10 - 1
03/11 - 1
03/12 - 1
03/13 - 1
03/14 - 1
03/15 - 1
03/16 - 1
03/17 - 1
03/18 - 1
03/19 - 1
03/20 - 1
03/21 - 1
03/22 - 1
03/23 - 1
03/24 - 1
03/25 - 1
03/26 - 1
03/27 - 1
03/28 - 1
03/29 - 1
03/30 - 1
03/31 - 1
03/32 - 0
04/00 - 0
04/01 - 1
04/02 - 1
04/03 - 1
04/04 - 1
04/05 - 1
04/06 - 1
04/07 - 1
04/08 - 1
04/09 - 1
04/10 - 1
04/11 - 1
04/12 - 1
04/13 - 1
04/14 - 1
04/15 - 1
04/16 - 1
04/17 - 1
04/18 - 1
04/19 - 1
04/20 - 1
04/21 - 1
04/22 - 1
04/23 - 1
04/24 - 1
04/25 - 1
04/26 - 1
04/27 - 1
04/28 - 1
04/29 - 1
04/30 - 1
04/31 - 0
04/32 - 0
05/00 - 0
05/01 - 1
05/02 - 1
05/03 - 1
05/04 - 1
05/05 - 1
05/06 - 1
05/07 - 1
05/08 - 1
05/09 - 1
05/10 - 1
05/11 - 1
05/12 - 1
05/13 - 1
05/14 - 1
05/15 - 1
05/16 - 1
05/17 - 1
05/18 - 1
05/19 - 1
05/20 - 1
05/21 - 1
05/22 - 1
05/23 - 1
05/24 - 1
05/25 - 1
05/26 - 1
05/27 - 1
05/28 - 1
05/29 - 1
05/30 - 1
05/31 - 1
05/32 - 0
06/00 - 0
06/01 - 1
06/02 - 1
06/03 - 1
06/04 - 1
06/05 - 1
06/06 - 1
06/07 - 1
06/08 - 1
06/09 - 1
06/10 - 1
06/11 - 1
06/12 - 1
06/13 - 1
06/14 - 1
06/15 - 1
06/16 - 1
06/17 - 1
06/18 - 1
06/19 - 1
06/20 - 1
06/21 - 1
06/22 - 1
06/23 - 1
06/24 - 1
06/25 - 1
06/26 - 1
06/27 - 1
06/28 - 1
06/29 - 1
06/30 - 1
06/31 - 0
06/32 - 0
07/00 - 0
07/01 - 1
07/02 - 1
07/03 - 1
07/04 - 1
07/05 - 1
07/06 - 1
07/07 - 1
07/08 - 1
07/09 - 1
07/10 - 1
07/11 - 1
07/12 - 1
07/13 - 1
07/14 - 1
07/15 - 1
07/16 - 1
07/17 - 1
07/18 - 1
07/19 - 1
07/20 - 1
07/21 - 1
07/22 - 1
07/23 - 1
07/24 - 1
07/25 - 1
07/26 - 1
07/27 - 1
07/28 - 1
07/29 - 1
07/30 - 1
07/31 - 1
07/32 - 0
08/00 - 0
08/01 - 1
08/02 - 1
08/03 - 1
08/04 - 1
08/05 - 1
08/06 - 1
08/07 - 1
08/08 - 1
08/09 - 1
08/10 - 1
08/11 - 1
08/12 - 1
08/13 - 1
08/14 - 1
08/15 - 1
08/16 - 1
08/17 - 1
08/18 - 1
08/19 - 1
08/20 - 1
08/21 - 1
08/22 - 1
08/23 - 1
08/24 - 1
08/25 - 1
08/26 - 1
08/27 - 1
08/28 - 1
08/29 - 1
08/30 - 1
08/31 - 1
08/32 - 0
09/00 - 0
09/01 - 1
09/02 - 1
09/03 - 1
09/04 - 1
09/05 - 1
09/06 - 1
09/07 - 1
09/08 - 1
09/09 - 1
09/10 - 1
09/11 - 1
09/12 - 1
09/13 - 1
09/14 - 1
09/15 - 1
09/16 - 1
09/17 - 1
09/18 - 1
09/19 - 1
09/20 - 1
09/21 - 1
09/22 - 1
09/23 - 1
09/24 - 1
09/25 - 1
09/26 - 1
09/27 - 1
09/28 - 1
09/29 - 1
09/30 - 1
09/31 - 0
09/32 - 0
10/00 - 0
10/01 - 1
10/02 - 1
10/03 - 1
10/04 - 1
10/05 - 1
10/06 - 1
10/07 - 1
10/08 - 1
10/09 - 1
10/10 - 1
10/11 - 1
10/12 - 1
10/13 - 1
10/14 - 1
10/15 - 1
10/16 - 1
10/17 - 1
10/18 - 1
10/19 - 1
10/20 - 1
10/21 - 1
10/22 - 1
10/23 - 1
10/24 - 1
10/25 - 1
10/26 - 1
10/27 - 1
10/28 - 1
10/29 - 1
10/30 - 1
10/31 - 1
10/32 - 0
11/00 - 0
11/01 - 1
11/02 - 1
11/03 - 1
11/04 - 1
11/05 - 1
11/06 - 1
11/07 - 1
11/08 - 1
11/09 - 1
11/10 - 1
11/11 - 1
11/12 - 1
11/13 - 1
11/14 - 1
11/15 - 1
11/16 - 1
11/17 - 1
11/18 - 1
11/19 - 1
11/20 - 1
11/21 - 1
11/22 - 1
11/23 - 1
11/24 - 1
11/25 - 1
11/26 - 1
11/27 - 1
11/28 - 1
11/29 - 1
11/30 - 1
11/31 - 0
11/32 - 0
12/00 - 0
12/01 - 1
12/02 - 1
12/03 - 1
12/04 - 1
12/05 - 1
12/06 - 1
12/07 - 1
12/08 - 1
12/09 - 1
12/10 - 1
12/11 - 1
12/12 - 1
12/13 - 1
12/14 - 1
12/15 - 1
12/16 - 1
12/17 - 1
12/18 - 1
12/19 - 1
12/20 - 1
12/21 - 1
12/22 - 1
12/23 - 1
12/24 - 1
12/25 - 1
12/26 - 1
12/27 - 1
12/28 - 1
12/29 - 1
12/30 - 1
12/31 - 1
12/32 - 0
13/00 - 0
13/01 - 0
13/02 - 0
13/03 - 0
13/04 - 0
13/05 - 0
13/06 - 0
13/07 - 0
13/08 - 0
13/09 - 0
13/10 - 0
13/11 - 0
13/12 - 0
13/13 - 0
13/14 - 0
13/15 - 0
13/16 - 0
13/17 - 0
13/18 - 0
13/19 - 0
13/20 - 0
13/21 - 0
13/22 - 0
13/23 - 0
13/24 - 0
13/25 - 0
13/26 - 0
13/27 - 0
13/28 - 0
13/29 - 0
13/30 - 0
13/31 - 0
13/32 - 0
Upvotes: 3
Reputation: 60007
For all months (i.e. 28 days):
(0[1-9]|1[0-2])/([01][1-9]|10|2[0-8])
For all months apart from February:
(0[13-9]|1[0-2])/(29|30)
For months with 31 days (i.e.01, 03, 05,07, 08, 10 and 12)
(0[13578]|1[0-2])/31
Putting this together
((0[1-9]|1[0-2])/([01][1-9]|10|2[0-8]))|((0[13-9]|1[0-2])/(29|30))|((0[13578]|1[0-2])/31)
Then for leap years append the following to the regular expression
|02/29
Upvotes: 12
Reputation: 208505
Regex is not the right answer for this, use a date library to perform this validation.
That being said, here is a regex that should do the trick (this should hopefully help to convince you that regex is not the right answer):
^(0[13578]|1[02])/([012]\d|3[01])|(0[469]|11)/([012]\d|30)|02/[012]\d$
If you just need simple validation that doesn't check for number of days in a month, you could use the following:
^(0[1-9]|1[012])/([012]\d|3[01])$
Upvotes: 0
Reputation: 32807
You can avoid regex
and instead use method like split
So,you must follow these steps
1>Split
the string with /
2>Parse the resulting MM
and DD
to an integer
3>now compare
parsed integers to check whether it's a valid month or day..
Upvotes: 2
Reputation: 1738
I think this might work
^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$
Upvotes: 0