With form validation: why onsubmit=”return functionname()” instead of onsubmit=”functionname()”?

You need the return so the true/false gets passed up to the form’s submit event (which looks for this and prevents submission if it gets a false).

Lets look at some standard JS:

function testReturn() { return false; }

If you just call that within any other code (be it an onclick handler or in JS elsewhere) it will get back false, but you need to do something with that value.


In that example the return value is coming back, but nothing is happening with it. You’re basically saying execute this function, and I don’t care what it returns. In contrast if you do this:

var wasSuccessful = testReturn();

then you’ve done something with the return value.

The same applies to onclick handlers. If you just call the function without the return in the onsubmit, then you’re saying “execute this, but don’t prevent the event if it return false.” It’s a way of saying execute this code when the form is submitted, but don’t let it stop the event.

Once you add the return, you’re saying that what you’re calling should determine if the event (submit) should continue.

This logic applies to many of the onXXXX events in HTML (onclick, onsubmit, onfocus, etc).

Leave a Comment