Sarmad Gardezi

Sarmad Gardezi

a freelance developer

Home about Portfolio Blog

How to disable 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.


Developer: Sarmad Gardezi


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

Hope the above helped. You can Download or get the Whole Page Code From Here



Video Tutorials