Question

I have this javascript code:

<script type="text/javascript">
<!--
function testCode () {
  if (checkCode (document.getElementById('Code').value,document.getElementById('Type').value)) {
      return true;
  } 
  else {alert (Errors[ErrorNo])};
}
//-->
</script>

And my form:

   <form accept-charset="UTF-8" action="verify.php" onsubmit="return testCode()" method="post">
   // My stuff
  <select id="Type" name="Type">
    <option value="basic">Basic</option>
    <option value="silver">Silver</option>
    <option value="gold">Gold</option>
    <option value="platinum">Platinum</option>
  </select>

  <input name="Code" type="text" id="Code">
  <input type="submit" value="Submit">
  </form>

After I submit the form, for example if I do not enter the code, will alert me with the error, but still submit the form, and I want to not submit it if the form is not validated.

Was it helpful?

Solution

use return false; if validation fails...

function testCode () {
  if (checkCode (document.getElementById('Code').value,document.getElementById('Type').value)) {
      return true;
  } 
  else {
      alert (Errors[ErrorNo]);
      return false;
      }
}

OTHER TIPS

You need to return false;

else {alert (Errors[ErrorNo]); return false;};

This will prevent execution of the rest of the events.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top