How does this validation script work? <p>I have the following validation script that works fine, but I don't understand how it works and need your help with it.</p> <p>The HTML,</p> <pre><code>&lt;form action="xyz.php" method="post"&gt; &lt;div class="form-group"&gt; &lt;label class="sr-only" for="id"&gt;Enter ID&lt;/label&gt; &lt;input type="text" class="form-control" name="id" id="id" required&gt; &lt;/div&gt; &lt;button onclick="return validateID()" type="submit" name="submit" class="btn"&gt;Submit&lt;/button&gt; &lt;/form&gt; </code></pre> <p>And this is the JS,</p> <pre><code>&lt;script&gt; function validateID() { var id = document.getElementById("id").value; var regL= new RegExp("^([0-9][0-9][0-9])$"); if (!( (regL.test(id)) )) { window.location.href = "index-iderror.php"; return false; } else { return true; } } &lt;/script&gt; </code></pre> <p>Some questions that I have about this,</p> <ol> <li>What happens when false or true is returned to onclick? Does it decide whether the form data is or is not sent to xyz.php?</li> <li>How does the execution continue till the line <code>return false</code> after <code>window.location.href</code>? Shouldn't <code>window.location.href</code> direct to a new page stopping execution? The script does not work if <code>return false</code> is removed i.e. the unchecked form data is sent to xyz.php.</li> </ol>