title: Should I validate my form twice?
body: <p>My code works perfectly <a href="http://jsfiddle.net/vPSm6/4/" rel="nofollow">here</a>.<br>
  But the problem is that I want to add some code validation so that the form can't be submitted if something is wrong in the code. Here is the code I added:  </p>
  
  <h1>Jquery code:</h1>
  
  <pre><code> var user = document.getElementById('u');
  var email = document.getElementById('em');
  var pass1 = document.getElementById('p1');
  var pass2 = document.getElementById('pa2');
  function isEmpty(input) {
    if (input.value == "" || input.value == null) {
      return true;
    } else {
      return false;
    }
  }
  
  function validateform() {
    if(isEmpty(user) || isEmpty(email) || isEmpty(pass1) || isEmpty(pass2))
    {
      alert("All fields are required.");
      $("#form").submit(function(event) {
        event.preventDefault();
      });
  
      if(isEmpty(user))
      {  
        user.focus();
      }
      else if(isEmpty(email))
      {  
        email.focus();
      }
      else if(isEmpty(pass1))
      {  
        pass1.focus();
      }
      else if(isEmpty(pass2))
      {  
        pass2.focus();
      }
    }
  }
  </code></pre>
  
  <p>I also added an Id to my form:</p>
  
  <pre><code>&lt;form action="m.php" method="post" id="form"&gt;
  </code></pre>
  
  <p>I also added the onsubmit here:</p>
  
  <pre><code>&lt;input name="submit" type='submit' value='Submit' onsubmit="validateform()"&gt;  
  </code></pre>
  
  <p>but it is not working, the page just moves to <code>m.php</code> even if all fields are empty. what should I do? should I install the jquery validating plugin and validate twice?</p>
  
  <p><strong>Edit</strong><br>
  Here is a <a href="http://jsfiddle.net/vPSm6/5/" rel="nofollow">Demo</a></p>