Validate aspx texbox through Javascript - undefined or null reference

I am trying to validate a aspx textbpx with Javascript and am getting following error when if statement is executed in Javascript:

0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference

The following is the function in JavaScript in aspx page:

<script type="text/javascript">
 function myFunction(var1)
 {
 if (document.getElementById("<%=TextBox1.ClientID%>").value == "") 
 {
     alert("Please enter something");
     document.getElementByID("<%=TextBox1.ClientID%>").focus();
 }
 else
{
 alert("Text box has value");
}
}
</script>

Following is aspx:

<div id="Div1">
    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="myFunction('TextBox1')" />
<asp:TextBox ID="TextBox1" runat="server" ClientIDMode="Static">TextBox Data</asp:TextBox>

I have tried, "ValueOf", "InnerText" also. I have also tried passing TextBox1 as avriable and still doesn't work.

Does anyone know why I am unable to access TextBox1 from JavaScript function???

Thank you

Answers


Try this

 if(document.getElementById('<%= yourtextboxid.ClientID%>').value != "")
     { 
       alert("has value!")
     } else {
      alert("please enter some thing");
      document.getElementById('<%= yourtextboxid.ClientID%>').focus();
      }

Need Your Help

Java UTF-8 special characters to JavaScript

javascript java encoding utf-8

I have a Java Web Application(Webwork) and I have some .jsps which show some data that can be special (UTF-8) characters. Ex:Jérôme Serrano

Best way to giving ipa file for testing to several user

ios

I have exported the ipa file using adhoc. I want to give this file to multiple user without testflight.