My preference is for a combination of a simple regular expression (\d{1,2})[-/.](\d{1,2})[-/.](\d{4}) with some code that confirms that this is really the right date. You will need to have this code anyway if you do not want to make a monstrous regular expression that rejects "29-02-2011" but not "29-02-2008".
Anyway, here is a breakdown of this regex so you can see what happens:
\d{1,2} : this part corresponds to one or two ( {1,2} ) digits ( \d ), making up the daily part of the date.[-/.] : this corresponds to one of the characters inside the brackets, that is, a . , a / or - .\d{1,2} : again, this corresponds to one or two digits from the month.[-/.] : another delimiter ...\d{4} : this corresponds to four digits ( {4} ) for part of the year.
Note that in parentheses is the portion of the day, month, and year of the regular expression. This is done to create groups. Each of these three parts will be recorded in a group that you can get from the match. Groups are identified by a number, starting from 1, from left to right. This means that the day will be group 1, group of months 2 and group of 3 years. There is also group 0, which always contains all the text matched.
You can use groups to perform the second part of the check and reject invalid dates like "30-02-2011", "31-4-2011" or "32-13-2011".
If you want to reject input that uses two different delimiters, for example, "12/31/2011", you can use a slightly more advanced function called backreferences:
(\d{1,2})([-/.])(\d{1,2})\2(\d{4})
Notice that now I have placed the first delimiter inside the group. This changes the month to group 3, and the year to group 4. The separator is mapped to group 2. The exception is \2 part between month and year. It corresponds to all that corresponded to the second previous group. If you release two groups from the backlink, you will end up in group 2, the delimiter. If this group matches a . , backreference matching only a . ; if it matches a - , the backlink will match only - ; etc.
source share