onsubmit Event

Fires when a FORM is about to be submitted.

Syntax

Inline HTML<ELEMENT onsubmit = "handler" ... > All platforms
Event propertyobject.onsubmit = handlerJScript only
object.onsubmit = GetRef("handler")Visual Basic Scripting Edition (VBScript) 5.0 or later only
Named script <SCRIPT FOR = object EVENT = onsubmit> Internet Explorer only

Event Information

BubblesNo
CancelsYes
To invoke Submit a form using the INPUT TYPE=submit, INPUT TYPE=image, or BUTTON TYPE=submit object.
Default action Causes a form to be sent to whatever location is stipulated in the ACTION attribute of the form object.

Event Object Properties

Although event handlers in the DHTML Object Model do not receive parameters directly, a handler can query the event object for the following event properties.

Available Properties

altKey Retrieves a value that indicates the state of the ALT key.
altLeft Retrieves a value that indicates the state of the left ALT key.
ctrlKey Sets or retrieves the state of the CTRL key.
ctrlLeft Sets or retrieves the state of the left CTRL key.
returnValue Sets or retrieves the return value from the event.
shiftKey Retrieves the state of the SHIFT key.
shiftLeft Retrieves the state of the left SHIFT key.
srcElement Sets or retrieves the object that fired the event.
type Sets or retrieves the event name from the event object.

Remarks

You can override this event by returning false in the event handler. Use this capability to validate data on the client side to prevent invalid data from being submitted to the server. If the event handler is called by the onsubmit attribute of the form object, the code must explicitly request the return value using the return function, and the event handler must provide an explicit return value for each possible code path in the event handler function.

The submit method does not invoke the onsubmit event handler.

Example

This example shows how to use onsubmit on a form. You can return false to prevent the form from being submitted. Note the use of the return keyword in the onsubmit event handler.

<script type="text/javascript">
    
    function displayAnswer ()
    {
        var question = document.getElementById('question');
        var selected = -1;
        for (var i=0; i<question.answer.length; i++)
        {
            if (question.answer[i].checked) {
                selected = i;
                break;
            }
        }
        if (selected > -1) {
              alert("You answered:  " + question.answer[selected].value );
              return true;
        }

        alert("Please select an answer.");
        return false;
      }
    
</script>

<form id="question" action="#" name="question" onsubmit="return(displayAnswer())">
    <p style="font-size: xx-large">Are you a turtle?</p>
    <p><label><input name="answer" type="radio" value="YES" />YES</label></p>
    <p><label><input name="answer" type="radio" value="NO" />NO</label></p>
    <p><label><input name="answer" type="radio" value="MAYBE" />MAYBE</label></p>
    <p><input type="submit" value="SUBMIT" />&nbsp;&nbsp;
    <input type="reset" value="RESET" /></p>
</form>
This feature requires Microsoft Internet Explorer 5.5 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

This event is defined in HTML 4.0 World Wide Web link.

Applies To

FORM, HTMLFormElement Constructor

See Also

Tags :


Community Content

yecril
No return function

There is no such thing as the return function. JavaScript has a return statement.

Tags : contentbug

videogold
Fires after submit button is clicked
"Fires when a FORM is about to be submitted."
How metaphysically ambiguous! Exactly how does the form know when it is about to be submitted? A sixth sense?
This event occurs after the submit button is clicked, not "When the form is about to be submitted", whatever that means.

Page view tracker