我在这里看到了一个潜在的答案,但那是YYYY-MM-DD:JavaScript日期验证
我为MM-DD-YYYY修改了上面的代码,但我还是无法让它工作:
String.prototype.isValidDate = function()
{
var IsoDateRe = new RegExp("^([0-9]{2})-([0-9]{2})-([0-9]{4})$");
var matches = IsoDateRe.exec(this);
if (!matches) return false;
var composedDate = new Date(matches[3], (matches[1] - 1), matches[2]);
return ((composedDate.getMonth() == (matches[1] - 1)) &&
(composedDate.getDate() == matches[2]) &&
(composedDate.getFullYear() == matches[3]));
}
Run Code Online (Sandbox Code Playgroud)
如何让上述代码适用于MM-DD-YYYY,更好的是MM/DD/YYYY?
谢谢.