2011-11-04 155 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語句,現在它像魅力一樣工作。非常感謝。 –