title: How to solve validation using multiple buttons in a form
body: <p>I have a form, with a number of textboxes which a user can fill in. At the bottom of the form I have two buttons. One for canceling and one for submitting. Like the example below</p>
  
  <pre><code>&lt;form action='bla.php' method='post'&gt;
      &lt;input type='text' name='someTextField1'&gt;
      &lt;input type='text' name='someTextField2'&gt;
      &lt;input type='text' name='someTextField3'&gt;
      &lt;input type='submit' name='submit'&gt;
      &lt;input type='submit' name='cancel'&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And I have a js function that checks the fields for their data which I used to use for both buttons. I therefor refer to the js function in the form as below:</p>
  
  <pre><code>&lt;form action='bla.php' method='post' name='form' onSubmit='return CheckFields()'&gt;
  </code></pre>
  
  <p>The js function looks like this:</p>
  
  <pre><code>function CheckFields() {
      var formname = "form";
      var x = document.forms[formname]["someTextField1"].value;
      var result = true;
      var text = "";
      if (x == null || x == "") {
          text += "Dont forget about the someTextField1.\n";
          result =  false;
      }
  
      if(!result)
          alert(text);
      return result;
  }
  </code></pre>
  
  <p>Now I want this js function to only run when using the submit and not the cancel button. When I try to move the call to the function to the submit button as below it doesn't work: </p>
  
  <pre><code>&lt;input type='submit' name='submit' onClick='return CheckFields()'&gt;
  &lt;input type='submit' name='cancel'&gt;
  </code></pre>
  
  <p>Why? What is the smartest way of solving this? Should I leave the call to CheckFields() in the form and check within the script what button was clicked or should I remake the function to somewhat work with a button instead? Anyone have an idea or an example? </p>