且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式可以在javascript中以mm / dd / yyyy格式验证短和长日期

更新时间:2023-02-26 13:44:49

这是因为 12/05/20 您输入的子串 12/05 / 20-0 是有效的日期。

This happens because 12/05/20 which is a substring of your input 12/05/20-0 is a valid date.

为避免子串匹配,您可以使用锚点:

To avoid substring matches you can use anchors as:

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

但是,再次上述允许日期如 00/00/0000 29 / 02 / NON_LEAP_YEAR 无效。

But again the above allows dates such as 00/00/0000 and 29/02/NON_LEAP_YEAR which are invalid.

所以它更好的使用库函数做这个验证。

So its better to use a library function do this validation.

我找到了一个这样的图书馆: datajs

I was able to find one such library: datajs