Regex to validate date format dd/mm/yyyy

后端 未结 21 2622
挽巷
挽巷 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:13

    The best way according to me is to use the Moment.js isValid() method by specifying the format and use strict parsing.

    As moment.js documentation says

    As of version 2.3.0, you may specify a boolean for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly, including delimiters.

    value = '2020-05-25';
    format = 'YYYY-MM-DD';
    moment(value, format, true).isValid() // true
    

提交回复
热议问题