2013-10-08 57 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> 
相关问题