Check date format in JavaScript

老子叫甜甜 提交于 2020-08-11 03:03:47

问题


I would like to check if the date format is (dd/mm/yyyy) before the post is made in a JavaScript (jQuery).

How could I achieve that?


回答1:


You can use a regex:

/^\d{2}\/\d{2}\/\d{4}$/.test(str)

Of course this will not check for a valid date, but in that case you can always let your server-side code throw an error. If you want more client-side validation have a look at Javascript: how to validate dates in format MM-DD-YYYY?; it is easy to adapt to your format.




回答2:


i think that would be very difficult, considering the ambiguity for cases where the date is less than equal to 12



来源:https://stackoverflow.com/questions/10638405/check-date-format-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!