title: JavaScript validation on button click
body: <p>I'm facing a problem with JavaScript validation.</p>
  
  <p>I have the following source code:</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Full_Width.master" AutoEventWireup="true" CodeFile="validate.aspx.cs" Inherits="validate" %&gt;
  
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"&gt;
  &lt;script type="text/javascript"&gt;
  
  function emptelephoneno() {
      var elem = document.getElementById('&lt;%=txtfullname.ClientID%&gt;');
      if (elem.value.length == 0) {
          elem.style.border = '1px solid red';
          document.getElementById('nameError').innerHTML = "Please Enter Your First Name";
          return false;
      }
      else {
          elem.style.border = '';
          document.getElementById('nameError').innerHTML = "";
      }
      return true;
  }
  
  function emptelephoneno1() {
      var elem1 = document.getElementById('&lt;%=TextBox1.ClientID%&gt;');
      if (elem1.value.length == 0) {
          elem1.style.border = '1px solid red';
          document.getElementById('Span1').innerHTML = "Please Enter Your First Name";
          return false;
      }
      else {
          elem1.style.border = '';
          document.getElementById('Span1').innerHTML = "";
      }
  
      return true;
  }
  
  &lt;/script&gt;
  &lt;/asp:Content&gt;
  
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"&gt;
  &lt;div class="wrapper row3"&gt;
      &lt;div id="container" class="clear"&gt;
          &lt;div id="content" style="border:1px solid green; margin-left:10px; width:600px;"&gt;
              &lt;div class="divRegistration"&gt;
                  &lt;div class="divleft"&gt;
                      &lt;label&gt; full name :&lt;span class="spanstar"&gt;*&lt;/span&gt;&lt;/label&gt;
                  &lt;/div&gt;
              &lt;div class="divright"&gt;
                  &lt;asp:TextBox ID="txtfullname" runat="server" MaxLength="30"&gt;&lt;/asp:TextBox&gt;
                  &lt;span id="nameError" class="spanerror"&gt;&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="divRegistration"&gt;
              &lt;div class="divleft"&gt;
              &lt;label&gt; full name :&lt;span class="spanstar"&gt;*&lt;/span&gt;&lt;/label&gt;
          &lt;/div&gt;
          &lt;div class="divright"&gt;
              &lt;asp:TextBox ID="TextBox1" runat="server" MaxLength="30"&gt;&lt;/asp:TextBox&gt;
              &lt;span id="Span1" class="spanerror"&gt;&lt;/span&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;asp:Button ID="btn" runat="server" text="submit" OnClientClick="return emptelephoneno();return emptelephoneno1();" 
          onclick="btn_Click" /&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>And in aspx.cs page I have written the following code:</p>
  
  <pre><code>this.txtfullname.Attributes.Add("onblur", "javascript:emptelephoneno();");
  this.TextBox1.Attributes.Add("onblur", "javascript:emptelephoneno1();");
  </code></pre>
  
  <p>The first validation actually works when I click on the button, but the second one doesn't.</p>
  
  <p>Can you provide any suggestions, please?</p>