Javascript check for empty text field on button click in asp.net

I have an asp.net application with one panel. Inside this panel, I have one image button and textbox. I wrote a javascript validation function for a text field that displays a warning window for entering some values ​​in textbox. Now this function does not work, giving an error at runtime:

Required Object

My code is here:

<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>

My Javascript Function:

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

Please suggest a solution for this.

Thanks for the advanced.

+4
source share
3 answers
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;
        }
}
+2
source

Try the following:

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

document.getElementById , ( ) .

- , .

: , , TRIM. ( ).

+3
<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>
0
source

All Articles