• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

8. Dynamic Forms > 8.6. Blocking Submissions from the Enter Key

Blocking Submissions from the Enter Key

NN 4(only), IE 4

Problem

You want to prevent a press of the Enter/Return key from submitting the form.

Solution

The default behavior of most browsers (but this is not specified in any HTML standard) is that a form consisting of a single text input element submits the form when the field has focus and the user presses the Enter/Return key. Although it is a convenience for some forms, it may prematurely submit a form that has other kinds of elements that need attention.

Use the keyboard event models and the onkeydown event handler of the text box to ignore the Enter/Return key:

function blockEnter(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode :
        ((evt.which) ? evt.which : evt.keyCode);
    if (charCode =  = 13 || charCode =  = 3) {
        return false;
    } else {
        return true;
    }
}
...
<input type="text" name="search" size="40" onkeydown="return blockEnter(event)" />

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint