[Javascript] Press enter submits form

Harry Love hlove at u.washington.edu
Tue Aug 27 14:14:02 CDT 2002


<snip>
Yeah but that's why I want to use Javascript to prevent that- I'm pretty
sure it's possible. Something like on key press submit() or something.
But
maybe it's not possible?
</snip>

Ah, preventing!  Well, if the purpose is to prevent people from
prematurely submitting the form, you could also use the onsubmit event
handler to validate the forms.  Roughly, onsubmit, if a form field is
incomplete, alert the user to complete the form, put focus on the empty
text field, etc.  Or, confirm a submission using onsubmit="confirm('Do
you want to submit the form?')".

And you're right, you can use onkeydown, onkeypress, and onkeyup to pass
the event object to a function from a text input or textarea.  The
[enter] key is captured by the event object (Decimal Character Code 13).
I've never tried it myself, but a Google search appears to produce
favorable results.  A search for "javascript enter key" produced this
(sorry if the URL wraps):
http://www.google.com/search?sourceid=navclient&q=javascript+enter+key
The second link looks like a winner.

Regards,
Harry




More information about the Javascript mailing list