2009-07-01 22 views
1

我想用onBlur事件在文本框上進行日期檢查。我不知道如何檢查在ASPX的JavaScript文本框。這是我到目前爲止我可以使用onBlur事件對文本框執行日期檢查嗎?

TodayDate= new Date(); 
function checkEnteredDate() { 
if (document.getElementById('txtDate') > TodayDate) { 
alert("You cannot select a date later than today."); 
document.getElementById(TodayDate); 
} 
} 

這已經是一個javascript函數,我只是無法獲取文本框中的值作爲比較。 有什麼建議嗎?

回答

2

你可以嘗試通過的 「本」 的功能:

<asp:TextBox ID="Text1" onblur="CheckEnteredDate(this);" runat="server" /> 

編輯:這裏的JavaScript函數如何使用(大約):

function CheckEnteredDate(passed) { 
    if (new Date(passed.value) > new Date()) { 
     alert('Date greater than today'); 
    } 
} 
+0

這將很好地工作,沒有「javascript:」前綴 – Kamarey 2009-07-01 19:37:29

-1

你應該可以添加一個函數調用到這個文本框的onBlur事件。

+0

的功能已經被解僱。我只是不知道如何調用文本框中的內容 – MrM 2009-07-01 19:26:14

-1

當您將文本框傳遞給document.getElementById時,它將返回一個HTML對象,而不是文本框中的文本。使用value屬性獲取用戶輸入的值。請看下圖:

function checkEnteredDate() 
{ 
    var inputDate = document.getElementById('txtDate'); 
    if(inputDate == '') 
    { 
     alert('You must specify date.'); 
     return false; 
    } 

    inputDate = new Date(inputDate); 
    var today = new Date(); 
    today.setHours(0, 0, 0, 0); //By default today's date will have time portion as well. 

    if(inputDate > today) 
    { 
     alert('You can not select a date later than today'); 
     return false; 
    } 

    return true; 
} 
0

使用DateJs庫在客戶端進行日期驗證,如下所示...

function checkEnteredDate() { 
    var elem = document.getElementById('txtDate'); 

    if(Date.parse(elem.value) > Date.today()) { 
     alert("You cannot select a date later than today."); 
     elem.select(); 
    } 
} 
0

如果您使用Microsoft Ajax,日期解析已由所提供的JavaScript參考庫處理。

<asp:TextBox ID="Text1" onblur="CheckEnteredDate(this);" runat="server" /> 
然後在函數調用

function CheckEnteredDate(passed) { 
    var value = Date.parseLocale(passed.value, 'd'); 
    if (isNaN(value)) 
     alert('Not a valid date.'); 
    if (value > new Date()) 
     alert('You cannot select a date later than today.'); 
} 
相關問題