Back to the tool

Regex for MM DD YYYY Date Validation: The Pattern I Use, and Where It Stops

Regex for MM DD YYYY date validation is one of those problems that looks solved in one line and then quietly accepts February 31. The pattern below is the one I use, and the second step is the part most people skip.

Every date input on the web eventually meets the same question: is this string shaped like a date, and is it actually a date. Those are two different questions, and regex answers only the first one well. I learned this the embarrassing way, watching a form accept 02/31/2025 without complaint, because the pattern I had written checked that the month was two digits and the day was two digits, and never asked whether February has a 31st. The user was not trying to break anything. They had mistyped, and my validation had waved them through.

Here is the pattern I use now for strict MM/DD/YYYY input with slash separators. I will break it apart after, because a regex you cannot explain is a regex you cannot debug.

In a code block, it reads: ^(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d$

Regex for MM DD YYYY date validation, piece by piece

The anchors, ^ at the start and $ at the end, matter more than they look. Without them, a string like xx03/15/2026yy can match in the middle and pass. With them, the whole string must be a date and nothing else. The month group, (0[1-9]|1[0-2]), allows 01 through 09 or 10 through 12, and rejects 00 and 13. The day group, (0[1-9]|[12][0-9]|3[01]), allows 01 through 31 and rejects 00 and 32. The year group, (19|20) followed by two digits, allows 1900 through 2099. Widen it to four digits if your data needs earlier or later years, but do it deliberately, because a year range is a business rule hiding in a pattern.

Test it against a small table before you trust it. 03/15/2026 passes. 12/31/2026 passes. 13/01/2026 fails on the month. 03/32/2026 fails on the day. 02/31/2026 passes, and that pass is the point of this article. 02/29/2024 passes, and it happens to be a real date, because 2024 is a leap year. 02/29/2025 also passes the pattern, and it is not a real date, because 2025 is not a leap year. A pattern that cannot tell 2024 from 2025 on leap years is not broken. It is doing the job it was built for, which is shape, not calendar.

Variants are easy once the core is clear. To allow single digit months and days, like 3/5/2026, add an optional zero: (0?[1-9]|1[0-2]) for the month and (0?[1-9]|[12][0-9]|3[01]) for the day. To allow hyphens or dots as well as slashes, replace each slash with a character class like [/.-], but be careful: a class used twice allows mixed separators, so 03/15-2026 would pass unless you capture the first separator and backreference it. For most forms, I pick one separator and enforce it, because forgiving input is a user experience decision, not a regex trick, and it is better made in the form design than in the pattern.

The second step everyone skips

After the regex passes, parse the date for real. In JavaScript, split on the slash, build a date from the three numbers, and check that the resulting date month, day, and year match what you put in. If you asked for February 31 and the date object quietly rolled over to March 3, the check fails, and that failure is the validation working. In Python, datetime with the same three numbers raises an error for impossible dates, which is even cleaner. Either way, the regex is the bouncer at the door and the parser is the person who checks the ID. You need both, because the bouncer only checks the shape of the card.

There is a tempting alternative, which is to write a giant regex that encodes month lengths and leap years. It exists, it is long, and I do not recommend it. A pattern that long is hard to read, hard to test, and easy to break with a one character edit six months from now. The two step approach, regex for format plus parser for reality, is shorter, clearer, and testable in plain language: this string looks like a date, and this date exists. When a future developer reads your validation code, that sentence is what they need to find.

Frequently asked questions

What is the regex for MM DD YYYY date validation?

^(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d$ checks a two digit month from 01 to 12, a two digit day from 01 to 31, and a year from 1900 to 2099, with slash separators. It validates format and ranges, not real calendar dates.

Does this regex reject February 31?

No. February 31 matches the pattern, because the pattern only knows that 31 is a valid day number, not that February never has 31 days. A real date parser must reject it after the regex passes.

How do I allow dates without leading zeros, like 3/5/2026?

Use ^(0?[1-9]|1[0-2])/(0?[1-9]|[12][0-9]|3[01])/\d{4}$ where the 0? makes the leading zero optional for month and day. Decide whether your form wants strict two digit input or forgiving input before you pick.

Should I use regex alone to validate dates?

No. Use regex for format, then parse the captured month, day, and year with a date library or a manual range check that knows month lengths and leap years. Regex alone is a format filter, not a calendar.

What about DD MM YYYY instead of MM DD YYYY?

Swap the first two groups: ^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\d{4}$. The danger is ambiguity: 03/04/2026 means March 4 in the US format and April 3 in the European format, and no regex can tell you which one the user meant.

Related reading: Date Validation Regex Mistakes Worth Fixing · Regex to Validate Time in HH:MM Format · URL Validation Regex Mistakes · Regex Greedy vs Lazy Matching, Explained

Try the tool

Build a regex pattern with plain-English explanations.

Get new free tools by email

Want the next guide in your inbox? Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.