It is not a good idea to revive ancient threads - better to start a new thread of your own.
The name of the function is checkSubmit(). It returns true or false depending on the result of the validation.
It may be called in an onsubmit handler with onsubmit = checkSubmit().
That will cause the function checkSubmit to run when the form is submitted.
However, it is not enough to simply run the function as the form will submit regardless of whether the validation passed or failed. We need to use onsubmit = return checksubmit() to return the result of the function - true or false. If false is returned then the submit is cancelled. If true is returned then the submit takes place. OK?
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.