ASP.NET中JavaScript验证在按钮单击时文本框为空的情况

4

我有一个asp.net应用程序,其中包含一个面板。 在该面板内,我有一个图像按钮和一个文本框。 我编写了一个JavaScript验证函数,用于对文本框进行验证,如果没有输入值,则会显示警告框。 现在此函数无法工作并报运行时错误:

对象必需

我的代码如下:

<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");
    }
}

请为此提供解决方案。

只需修改 ImageButton 中的 OnClientClick="fnCheck" - user2660112
3个回答

3
试试这个:

尝试这个:

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

document.getElementById 方法获取运行时生成的 ID,该 ID 与服务器端 ID 不同(但不总是)。

一种方法是像我这样使用黄色代码。

另外,请考虑使用 TRIM 方法(如果需要处理它)。


3
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;
        }
}

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接