2011-11-04 106 views
0

所以我让我的代码在Chrome中正常工作。在FF中,我获得了前两个警报的日期,并且它们的格式正确,但是如果选择过去的日期,它不会抛出第三个警报。在IE中我可以得到所有的三个警报,但即使我选择了未来的日期。正如我所说的,Chrome的工作原理应该如此,如果任何人都可以提供一些洞察力,让它在整个工作过程中发挥出色,那就很好。JavaScript验证的跨浏览器问题

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> 
+0

http://grover.open2space.com/jquery.dates/home什么? – Jerome

+1

您可能希望使用'new Date(year,month,day [,hour,minute,second,millisecond])'构造函数,而不是依赖日期的字符串表示形式的构造函数。 https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date – Prusse

+0

@jeromeG这看起来像一个日历控件?我不想为此项目使用日历控件。尽管感谢您的链接,我可能会在未来的项目中使用它。 –

回答

0

您的日期是无效的FF,使用"/"到位"-"selecteddatetoday

或大约只用

 if(selecteddate < today) 

,而不是

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

这很好。我曾尝试做第一个,如果你在前期提供的statemet中,但仍然有日期的“ - ”。我将日期更改为“/”,并更改了if语句,现在它像魅力一样工作。非常感谢。 –