title: Javascript validation overwrites ASP.NET validators
body: <p>I have a problem that is making me crazy. On my page I have one <strong>Javascript validation</strong> and two <strong>ASP.NET validators</strong>. The validation outcome depends just on the result of the Javascript. That means if the Javascript returns <strong>true</strong> the ASP.NET validators are not checked.</p>
  
  <p>The <strong>Javascript</strong> code is:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      function Validate() {
          var ddlObj = document.getElementById('&lt;%=ddStatus.ClientID%&gt;');
          var txtObj = document.getElementById('&lt;%=txtComment.ClientID%&gt;');
          if (ddlObj.selectedIndex != 0) {
              if (txtObj.value == "") {
                  alert("Any change of Status requires a comment!");
                  txtObj.focus();
                  return false;
              }
          }
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>Instead the two <strong>ASP.NET validators</strong> are:</p>
  
  <pre><code>&lt;td&gt;&lt;asp:TextBox runat="server" ID="txtSerialNr" &gt;&lt;/asp:TextBox&gt;
  &lt;asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" ControlToValidate="txtSerialNr" ErrorMessage="***" /&gt;
                          &lt;/td&gt;
  
  &lt;td&gt;&lt;asp:TextBox runat="server" ID="txtProdName" &gt;&lt;/asp:TextBox&gt;
  &lt;asp:RequiredFieldValidator runat="server" ID="rfv1" ControlToValidate="txtProdName" ErrorMessage="***"&gt;&lt;/asp:RequiredFieldValidator&gt;&lt;/td&gt;
  </code></pre>
  
  <p>Anybody might help? Thanks</p>
  
  <p>UPDATE:</p>
  
  <p>I call the Javascript from a button:</p>
  
  <pre><code>    &lt;asp:Button runat="server" ID="btnSubmit" Text="Save New Product" 
  style="cursor:hand" OnClick="btnSubmit_Click" /&gt;
  </code></pre>
  
  <p>But I register the attribute from the <strong>code-behind:</strong></p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      btnSubmit.Attributes.Add("OnClientClick", "return Validate()");
  }
  </code></pre>