Regex to validate date format dd/mm/yyyy

后端 未结 21 2511
挽巷
挽巷 2020-11-21 06:32

I need to validate a date string for the format dd/mm/yyyy with a regular expresssion.

This regex validates dd/mm/yyyy, but not the invalid

21条回答
  •  情书的邮戳
    2020-11-21 07:15

    The following expression is nice and easy to manipulate:

    ((((0[13578]|1[02])(\/|-|.)(0[1-9]|1[0-9]|2[0-9]|3[01]))|((0[469]|11)(\/|-|.)(0[1-9]|1[0-9]|2[0-9]|3[0]))|((02)((\/|-|.)(0[1-9]|1[0-9]|2[0-8]))))(\/|-|.)(19([6-9][0-9])|20(0[0-9]|1[0-4])))|((02)(\/|-|.)(29)(\/|-|.)(19(6[048]|7[26]|8[048]|9[26])|20(0[048]|1[26])))
    

    It validates according to the MM/dd/YYYY format and allows for leap year support from 1960 to 2016. If you need the leap year support extended you need only manipulate this part of the expression:

    (19(6[048]|7[26]|8[048]|9[26])|20(0[048]|1[26]))
    

    Hope this helped you a lot

提交回复
热议问题