To prevent the form from submitting, I always used the onclick event to call the javascript method, which will do something, and then submit from there. You can also customize the form as follows:
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post"> First name: <input type="text" name="fname"> <input type="submit" value="Submit"> </form>
After submitting, the validateForm () method can prevent the submission if necessary:
function validateForm() { var x=document.forms["myForm"]["fname"].value if (x==null || x=="") { alert("First name must be filled out"); return false; } }
source share