2013-10-08 56 views
4

我有一個面板的asp.net應用程序。 在那個面板裏面我有一個圖像按鈕和textbox。 我已經爲文本框寫了一個javascript驗證函數,它將顯示用於在textbox中輸入一些值的警報框。 現在,這個功能不能正常工作了運行時錯誤:在asp.net按鈕點擊空文本框的Javascript驗證

Object required

我的代碼是在這裏:

<asp:Panel ID="pnlTop" runat="server"> 
    <tr height="35px" valign="top"> 
     <td align="right" valign="middle" colspan="2" height="50"> 
     <asp:ImageButton ID="imgbtnGOTO" runat="server" ToolTip="View Specific Record" BorderWidth="0" 
           ImageAlign="AbsMiddle" OnClientClick="javascript:return fnCheck()"></asp:ImageButton>&nbsp;&nbsp; 
     <asp:TextBox ID="txtPagingGoto" CssClass="clsTableCellLeft" Width="215px" runat="server" CausesValidation="true"></asp:TextBox> 
     </td> 
    </tr> 
</asp:Panel> 

我的JavaScript函數爲:

function fnCheck() { 
    if ((document.getElementById("txtPagingGoto").value).length == 0) { 
     alert("The textbox should not be empty"); 
    } 
} 

請建議此解決方案。

謝謝先進。

+0

只需修改'的OnClientClick = 「fnCheck」''中ImageButton' – 2013-10-08 10:50:16

回答

2
function fncheck() 
{ 
     var pgng = document.getElementById("<%=txtPagingGoto.ClientID%>").value.trim(); 
     if(pgnd == "") 
     { 
      alert('The textbox should not be empty...'); 
      document.getElementById("<%=txtfname.ClientID%>").focus(); 
      return false; 
     } 
} 
3

嘗試這種情況:

function fnCheck() { 
     if ((document.getElementById("<%=txtPagingGoto.ClientID%>").value).length == 0) { 
      alert("The textbox should not be empty"); 
    } 
} 

document.getElementById得到運行時生成 ID,其是不同的(不總是)從服務器端的ID。

一種方法是像我一樣使用黃色代碼。

另外:請考慮請使用TRIM方法。 (如果你需要處理它)。

0
<html> 
    <head> 
    <script type="text/javascript"> 
    function validate() 
     { 
     if(document.getElementById("aa").value=="") 
       { 
       alert("this textbox should not be empty"); 
       } 
     } 
    </script> 
    </head> 
    <body> 
<input type="txt" id="aa"/> 

    <input type="button" value="submit" onclick="validate()"/>` 

    </body> 
</html> 
相關問題