use addEventListener method to register event handler (i.e., checkBeforeSubmit function) to the <form> element for the ‘submit’ event. code example
Example: js form submit listener
document.getElementById("#myFormId").addEventListener("submit", function(e){
if(!isValid){
e.preventDefault(); //stop form from submitting
}
//do whatever an submit the form
});