Below is a script to allow you to run a precheck function in Javascript, preventing the form from submitting if you detect a problem.

First, in a separate, external Javascript file, copy the below script:

<!--

function checkMyForm(formID) {
   if (formID.elecments['cuconfirm'].checked == false) {
      alert('You have not ticked the box');
      return false;
   } else {
      formID.submit();
      return false;
   }
}

//->

The above code is checking a checkbox value in the sent form to see if it has been selected. If it has, then we are ok to submit the form, otherwise it displays the message box.

Now we need to tell the form to run this function instead of the normal submit.

To do this, change you form tag to add the onsubmit part to look like below.

<form name="testform" action="index.php" onsubmit="retun checkMyForm(this); return false;">

That’s really all there is to it. For the version I actually used, is changed the message box to display a hidden div on the form which was style red with css. I found this was a much nicer way of displaying the error box instead of the javascript alert box.

Tags: , ,

Join the conversation...

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.