JavaScript 验证的跨浏览器问题

发布于 2024-12-13 18:16:23 字数 873 浏览 0 评论 0原文

所以我的代码在 Chrome 中运行得很好。在 FF 中,我收到带有日期的前两个警报,它们的格式正确,但如果选择过去的日期,则不会引发第三个警报。在 IE 中,即使我选择未来的日期,我也会收到所有三个警报。正如我提到的,Chrome 可以正常工作,因此,如果有人能够提供一些见解,使其能够在所有浏览器上工作,那就太好了。

 function check()
{
   var selecteddate = document.form1.selectmonth.value + "-" + document.form1.selectday.value + "-" + document.form1.selectyear.value;
   var d = new Date();
   var today = (d.getMonth()+1) + "-" + d.getDate() + "-" + d.getFullYear();

   // I'd then make some alerts and it'd return the selected date and today with no problem
   alert(selecteddate);
   alert(today)
   //Now for the if statement is where it just stops working. I think maybe I'm doing
   //something wrong just solely in the if statement.
     if(new Date(selecteddate) < new Date(today))
       {
       alert("Past dates are not valid");
       return false;
       }
}
</script>

So I got my code to work just fine in Chrome. In FF I get the first two alerts with the dates and they're formatted properly and everything but if a past date is chosen it doesn't throw the third alert. In IE I get all three alerts but even if I choose a future date. As I mentioned Chrome works as it should so if anyone can provide some insight into making it work across them all that'd be great.

 function check()
{
   var selecteddate = document.form1.selectmonth.value + "-" + document.form1.selectday.value + "-" + document.form1.selectyear.value;
   var d = new Date();
   var today = (d.getMonth()+1) + "-" + d.getDate() + "-" + d.getFullYear();

   // I'd then make some alerts and it'd return the selected date and today with no problem
   alert(selecteddate);
   alert(today)
   //Now for the if statement is where it just stops working. I think maybe I'm doing
   //something wrong just solely in the if statement.
     if(new Date(selecteddate) < new Date(today))
       {
       alert("Past dates are not valid");
       return false;
       }
}
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2024-12-20 18:16:23

您的日期对 FF 无效,请使用 "/" 代替 selecteddatetoday"-"

或者只使用

       if(selecteddate < today)

而不是

  if(new Date(selecteddate) < new Date(today))

your dates are invalid to FF, use "/" in place of the "-" for selecteddate and today.

or what about just using

       if(selecteddate < today)

instead of

  if(new Date(selecteddate) < new Date(today))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文