title: How to not open a new window (JavaScript popup) if jQuery validation plugin returns false? (ASP.NET)
body: <p>I have an .aspx page with a couple of textfields. I validate these with jQuery validation plugin when the user clicks the submit button. When the submit button is clicked I also open up a new window, but I only want to open the window if the fields are correctly filled out.</p>
  
  <p>How do I stop the window from opening if the fields are not validated?</p>
  
  <p>.aspx.cs</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      btnSubmit.Attributes.Add("onClick", "return OpenPopup()");
  }
  </code></pre>
  
  <p>.aspx</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
          function OpenPopup() {
              window.open("AssessmentDocument.htm", "List", "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=230,height=140");
          }
  &lt;/script&gt;
  
  $("#AdviceForm").validate({
      errorPlacement: function (error, element) {
          error.insertBefore(element.prev());
      }
  });
  
  &lt;label&gt;Your Name *&lt;/label&gt;
  &lt;asp:TextBox ID="txtName" CssClass="required" runat="server"/&gt;
  &lt;br /&gt;
  
  &lt;label&gt;Your Email *&lt;/label&gt;
  &lt;asp:TextBox ID="txtEmail" CssClass="required email" runat="server"/&gt;
  
  &lt;asp:Button ID="btnSubmit" CssClass="adviceSubmit" runat="server" Text=""  onclick="btnSubmit_Click" /&gt;
  </code></pre>
  
  <p>What I want to do is that the user must enter their name and email and when they do, they can click the submit button (data gets inserted into a database) and get a popup with a link to a document to download. It doesn't have to be any security here really, if they check the page source code they can see the url to the popup but that's alright.</p>
  
  <p>If you suggest me to do it in another way please let me know.</p>
  
  <p>Edit: </p>
  
  <p>I updated the jQuery to</p>
  
  <pre><code>$(document).ready(function () {
      $("*[id$='btnSubmit']").attr("disabled", true); 
      $("#AdviceFOrm").validate({
          errorPlacement: function (error, element) {
              error.insertBefore(element.prev());
                  $("*[id$='btnSubmit']").removeAttr("disabled");
          }
  });
  </code></pre>
  
  <p>However, if the user enters something in the name field and at least one character in the email field it doesn't validate (Need to enter a valid email address) but I still get the pop up. If I don't enter something in the name field but a proper email it doesn't validate but I get the pop up. How do I need to alter the script?</p>
  
  <p>edit2:</p>
  
  <p>got it to work with this</p>
  
  <pre><code>    &lt;script type="text/javascript"&gt;
  
  
          $(document).ready(function () {
              $("#FreeBusinessAssessmentForm").validate({
                  errorPlacement: function (error, element) {
                      error.insertBefore(element.prev());
                          //disable the submit button
                  }
          });
  
           $('#lblResponse').fadeOut(8000, function () {
                  $(this).innerHTML("");
              });
          });
  
          function onClick() {
              if ($("#FreeBusinessAssessmentForm").valid())
                  return OpenPopup();
          }
  &lt;/script&gt;
  </code></pre>