Regular Expression to match a valid day in a date

后端 未结 10 1739
别那么骄傲
别那么骄傲 2020-12-11 01:45

I need help coming up with a regex to make sure the user enters a valid date The string will be in the format of mm/dd/yyyy

Here is what I have come up

10条回答
  •  伪装坚强ぢ
    2020-12-11 02:28

    ^(((((((0?[13578])|(1[02]))[\.\-/]?((0?[1-9])|([12]\d)|(3[01])))|(((0?[469])|(11))[\.\-/]?((0?[1-9])|([12]\d)|(30)))|((0?2)[\.\-/]?((0?[1-9])|(1\d)|(2[0-8]))))[\.\-/]?(((19)|(20))?([\d][\d]))))|((0?2)[\.\-/]?(29)[\.\-/]?(((19)|(20))?(([02468][048])|([13579][26])))))$
    

    From Expressions in category: Dates and Times

    Validates the correct number of days in a month, looks like it even handles leap years.

    You can of course change [\.\-/] with / to only allow slashes.

提交回复
热议问题