How do I validate a date in this format (yyyy-mm-dd) using jquery?

前端 未结 11 1338
长情又很酷
长情又很酷 2020-12-02 11:41

I am attempting to validate a date in this format: (yyyy-mm-dd). I found this solution but it is in the wrong format for what I need, as in: (mm/dd/yyyy).

Here is t

11条回答
  •  借酒劲吻你
    2020-12-02 11:48

    Rearrange the regex to:

    /^(\d{4})([\/-])(\d{1,2})\2(\d{1,2})$/
    

    I have done a little more than just rearrange the terms, I've also made it so that it won't accept "broken" dates like yyyy-mm/dd.

    After that, you need to adjust your dtMonth etc. variables like so:

    dtYear = dtArray[1];
    dtMonth = dtArray[3];
    dtDay = dtArray[4];
    

    After that, the code should work just fine.

提交回复
热议问题