Closed ypiguet-epfl closed 3 years ago
Regex is not the right tool to validate date inputs. Some calendar/date utility should be used instead.
A simple regex will fail for example for leap days: "2020-02-29" is a valid date but "2021-02-29" is not.
A real leap day: should be true
> /^(19|20)[0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|30|31)$/.test("2020-02-29")
true
Not a real date: should be false
> /^(19|20)[0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|30|31)$/.test("2021-02-29")
true
This regex is not used for validation. We will remove it from the documentation. Dates and DateTimes must follow ISO 8601.
@ypiguet-epfl Thanks again for raising the issue. I have updated the documentation.
The regexp suggested to validate dateOfBirth, vaccinationDate, and dateOfFirstPositiveTestResult, has at least 3 errors:
[19|20]
should be a parenthesized alternative(19|20)
. It actually accepts a valid year because the regexp doesn't begin with a boundary^
, but it doesn't reject"0000-01-01"
,"not born on 1980-01-01"
, or"|99-01-01"
./[19|20][0-9][0-9]-(0[1-9]|1[0-2])-([0-2][1-9]|3[0|1])/.test("1980-10-20")
is false instead of true./[19|20][0-9][0-9]-(0[1-9]|1[0-2])-([0-2][1-9]|3[0|1])/.test("1980-10-3|")
is true instead of false.Suggestion: Use the following regexp:
"^(19|20)[0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|30|31)$"
.