[1-9] # 01-09 or 1-9 | # .[0-9] # 10-19 or 20-29 | # .3 # 30, 31 ) #end of group #1 / # follow by a "/" ( # start of group #2 0?
[1-9] # 01-09 or 1-9 | # .1 # 10,11,12 ) # end of group #2 / # follow by a "/" ( # start of group #3 (19|20)\d\d # 19[0-9][0-9] or 20[0-9][0-9] ) # end of group #3 The above regular expression is used to validate the date format in “dd/mm/yyyy”, you can easy customize to suit your need.
is an open source component of Wrapp and is licensed under the MIT license.
There are no required external dependencies at all!
You want to validate dates in the traditional formats mm/dd/yy, mm/dd/yyyy, dd/mm/yy, and dd/mm/yyyy.
You want to use a simple regex that simply checks whether the input looks like a date, without trying to weed out things such as February 31You might think that something as conceptually trivial as a date should be an easy job for a regular expression. Because dates are such an everyday thing, humans are very sloppy with them. To somebody else, it may be the first working day of the year, if New Year’s Day is on a Friday.
It is unit tested with 100% code coverage and can be considered fit for production.
The other issue is that regular expressions don’t deal directly with numbers. That’s because the characters for the digits 0 through 9 occupy consecutive positions in the ASCII and Unicode character tables.
You can’t tell a regular expression to “match a number between 1 and 31”, for instance. See Chapter 6 for more details on matching all kinds of numbers with regular expressions.
We use capturing groups here because you’ll probably want to capture the day and month numbers anyway.
The final two solutions are a little more complex, so we’re presenting these in both condensed and free-spacing form.
“29/a/2008”, “a/02/2008” – month is invalid, day is invalid 6.