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

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

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 okay 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 your form tag to add the onsubmit part to look like below.

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 styled red with CSS. I found this was a much nicer way of displaying the error box instead of the javascript alert box.