Forum Moderators: open
<form method="post" action="yourscript.cgi" onSubmit="return false;">
<input type="submit" onClick="yourValidationRoutine(this.form);" value="Submit">
</form>
<script type="text/javascript">
function yourValidationRoutine(form) {
if (some_error) {alert(some_error); }
else { form.submit(); }
}
</script>
Here is what is happening: if Javascript is enabled, return false on submit will prevent the form from submitting via the submit button. For this reason, if it passes validation in yourValidationRoutine you must execute form.submit(); to submit the form.
If, however, Javascript is DISABLED, which is getting more and more common as users become wise to it's abuse, all Javascript commands will be ignored and the form will submit anyway. Many try to circumnavigate this by using the input type=button, but without Javascript those will do nothing. Then you have no orders or no contacts, and to the end user, your site is broken.
So you MUST use an input type=submit, and whatever Javascript screening you do in the browser you must replicate on the server side.
<form onsubmit="return checkform(this)" action=[..]>
<input type="text" name="myname">
<input type="submit" value="go!">
</form>
<script type="text/javascript">
funtion checkform(f) {
if (f.myname.value == '') {
alert('hey!');
return false;
} else {
return true;
}
}
</script>
[edited by: RonPK at 8:47 am (utc) on Aug. 27, 2006]