The form Submit method and event

Jake Howlett, 2 November 2000

Category: JavaScript; Keywords: Form submit onsubmit validate

Anybody with any programming experience starting to use JavaScript would expect that when they call a form's submit() method the corresponding onSubmit() event would be triggered. This is not the case.

For example, say that a document has the following function in its JS Header:

function FormSubmit( formObjecct ){
if ( formObject.Country.value=="") {
alert('You need to the Country field in');
formObject.Country.focus();
return false
} else {
return true
}
}


and the document has a form whose tag is:

<form name="_prj" onSubmit="FormSubmit( this )" method="post" action="....


This is all well and good as long as you use a "standard" submit button on the form:

<input type="submit" name="Submit" value="Submit This Form">


But if you needed to call this Submit method explicitly, from a function, button, anchor link etc then calling this method does not call the onSubmit() event:

<a href="JavaScript: document._prj.submit();">Submit Form</a>


In order to validate the form first the code would need to be:

if ( document._prj.onsubmit() ) document._prj.submit();


Hence:

<a href="JavaScript: if (document._prj.onsubmit()) document._prj.submit();">Submit Form</a>


Note: In R4.6 if you have a button that uses the following formula:

@Command([FileSave]); @Command([FileCloseWindow])


The resulting JavaScript calls the Submit method whilst ignoring the onSubmit event.

In R5 this has been rectified and the same button produces script that accounts for the onSubmit.