2012-11-20 75 views
0

我有一個文本框應用阿賈克斯日曆和應用了以下檢查其選擇如何查看日期不會比今天的ajax日曆大?

function checkDate(sender, args) { 
     if (sender._selectedDate > new Date()) { 
      alert("You cannot select a day future than today!"); 
      sender._selectedDate = new Date(); 
      // set the date back to the current date 
      sender._textbox.set_Value(sender._selectedDate.format(sender._format)) 
     } 
    } 

我的HTML代碼是:

<asp:TextBox ID="txtDOB" Width="140px" MaxLength="50" runat="server"></asp:TextBox> 
          <ajaxctrl:calendarextender onclientdateselectionchanged="checkDate" id="cale_txtDOB" 
           runat="server" targetcontrolid="txtDOB" format="MM/dd/yyyy" cssclass="cal_Theme1"> 
          </ajaxctrl:calendarextender> 

其工作正常,但如果我輸入的日期手動,那麼這是不工作,我怎麼能讓兩種方式爲我工作。

意味着如果用戶手動輸入它,那麼它也會檢查日期,以及用戶何時從日曆中選擇它,然後它也會驗證日期。

回答

1

這是修改後的JavaScript函數:

function checkDate(sender, args) { 
     //alert(sender._selectedDate > new Date()); 
     if (sender._selectedDate > new Date()) { 
      alert("You cannot select a day future than today!"); 
      sender._selectedDate = new Date(); 
      // set the date back to the current date 
      sender._textbox.set_Value(sender._selectedDate.format(sender._format)) 
      return false; 
     } 
    } 

這是一個新的JavaScript函數的,只是將它加入你的函數之下:

function checkDate1(txt) { 
     if (new Date(txt.value) > new Date()) { 
      alert("You cannot select a day future than today!"); 
      return false; 
     } 
    } 

現在轉到頁你的Page_Load事件,和添加以下代碼行:

this.txt.Attributes.Add("onblur", "checkDate1(this);"); 

您可以更改這些函數名稱,因爲我剛剛寫了這個你有點匆忙。沒有太多時間來測試它。如果您有任何問題,請告訴我。

0

您是否試過jQuery Change方法?

$('#textBoxName').change(function() { 
    if ($(this).val() > new Date()) { 
    alert("You cannot select a day future than today!"); 
     var today=new Date(); 
     $('#textBoxName').val(today); 
    } 
}); 
相關問題