开发者

validating asp.net textBox using Javascript

开发者 https://www.devze.com 2023-02-24 21:13 出处:网络
I have an asp.net TextBox control on my page and a search button whenever user clicks on the search button i want to validate the TextBox for Blank. i want to use a onClientClick event and pass the pa

I have an asp.net TextBox control on my page and a search button whenever user clicks on the search button i want to validate the TextBox for Blank. i want to use a onClientClick event and pass the parameter as my Javascript function will be going to be called from external JS.

Here's what I tri开发者_运维问答ed.

<asp:TextBox ID="search" runat="server">
</asp:TextBox>

<script language="javascript" type="text/javascript">
    function voidsearch(s) {
        alert(document.getElementById(s).value);
    }
</script>

<asp:ImageButton ID="img1" runat="server" ImageUrl="Dotnetnuke.ico" 
    OnClientClick="voidsearch('<%= search.ClientID %>'); return false;" />

but this is throwing error. Object Required. i also passed this.search.. but same error. i dont understand why i am getting this error as i have first declared control and then called its ID.

Please anyone help me for this.


It seems that the “search” TextBox is placed onto INamingContainer, so it is ClientID property cannot be evaluated. Use the approach, described in the infragistics get clientID of dropdown in Rowedit template thread:

<asp:TextBox ID="search" runat="server" OnInit="search_Init"></asp:TextBox>
<asp:ImageButton ID="img1" runat="server" ImageUrl="Dotnetnuke.ico" 
    OnClientClick="voidsearch();" return false;" />

protected void search_Init(object sender, EventArgs e) {
    TextBox txt = (TextBox)sender;
    string script = string.Format("var _{0} = document.getElementById('{1}');", txt.ID, txt.ClientID);
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "ANY_KEY", script, true);
}

<script language="javascript" type="text/javascript">
function voidsearch() {
    alert(_search);
    alert(_search.value);
}
</script>


Validate on blank text box on button click in asp.net

<asp:textbox id="name" runat="server"></asp:textbox>

create JavaScript function for validate textbox

<script language="javascript" type="text/javascript">
   function validate()
      {
         if(document.getElementByID("<%=name.ClientID%>").value=="")
           {
              alert("Please enter something");
              document.getElementByID("<%=name.ClientID%>").focus();
              return false;
           }
           return true;
      }
</script>

call function on button click:

<asp:button id="btn1" runat="server" OnClientClick="return validate()"></asp:button>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号