最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript validation for empty textbox on button click in asp.net - Stack Overflow

programmeradmin4浏览0评论

I have asp application with one panel. Inside that panel i have one image button and textbox. I have written a javascript validation function for textbox which will display alert box for entering some values in textbox. Now this function is not working gave run-time error:

Object required

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

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

Please suggest solution for this.

I have asp application with one panel. Inside that panel i have one image button and textbox. I have written a javascript validation function for textbox which will display alert box for entering some values in textbox. Now this function is not working gave run-time error:

Object required

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

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

Please suggest solution for this.

Share Improve this question edited May 31, 2018 at 10:35 Donald Duck 8,89223 gold badges79 silver badges102 bronze badges asked Oct 8, 2013 at 10:41 MonaliMonali 511 gold badge2 silver badges5 bronze badges 1
  • Just modify OnClientClick="fnCheck" in ImageButton – user2660112 Commented Oct 8, 2013 at 10:50
Add a ment  | 

3 Answers 3

Reset to default 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;
        }
}

Try this :

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

document.getElementById gets the runtime generated ID which is different(not always) from server side ID.

One way is to use the yellow code as I did.

Also : consider please to use the TRIM method. ( if you need to handle it).

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

  1. 暂无评论