JavaScript验证的跨浏览器问题

Cross Browser issue with JavaScript Validation

本文关键字:浏览器 问题 验证 JavaScript      更新时间:2023-09-26

所以我让我的代码在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>

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

或者直接用

       if(selecteddate < today)

代替

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