How to disallow Enter key on a form using JavaScript

You may have come across situations where you don’t want the HTML form to submit on pressing Enter key. The reason could be that you may have a long form which the user needs to fill and most users may have the habit of pressing Enter key when trying to move to the next field.

We will use JavaScript for this purpose. There is anĀ onkeypress event we will use for this purpose. We can setup a callback function against this event and check for the Enter key ASCII code and return false so that the action does not take place.

<script type="text/javascript">
function keypressAction(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
if (evt.keyCode == 13) { return false; }
}
document.onkeypress = keypressAction;
</script>

Hope the above helped.

If you would like to download the whole file with the code then please download
Disable Enter key using Javascript.

0.00 avg. rating (0% score) - 0 votes

Leave a Reply

Your email address will not be published. Required fields are marked *