>
 





The post Enter Key as the Default Button describes how to set the default behaviour for enter key press. However, sometimes, you need to disable form submission on Enter Key press. If you want to prevent it completely, you need to use OnKeyPress handler on <body> tag of your page.

<body OnKeyPress="return disableKeyPress(event)">

The javascript code should be:

<script language="JavaScript">

function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}

</script>

If you want to disable form submission when enter key is pressed in an input field, you must use the function above on the OnKeyPress handler of the input field as follows:

<input type=”text” name=”txtInput” onKeyPress=”return disableEnterKey(event)”>

Want automatic updates? Subscribe to our RSS feed or
Get Email Updates sent directly to your inbox!

Currently rated 4.1 by 10 people

  • Currently 4.1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Comments

November 16. 2007 06:54

gr8
it work fine
keep it up buddy
Thnks

sudhir | Reply

November 22. 2007 21:31

Thanks guys.

Keep up the good work!
I tried it and it worked for me. Woalla!

^_^

Cheers!

computervideos | Reply

December 11. 2007 06:54

thanks man! needed that.

Carl von Buelow | Reply

December 19. 2007 15:15

good site

asdasd | Reply

February 21. 2008 17:49

in IE it's ok ... but in Firefox 2.0.0.12 don't block enter key ... !!!! Cry

thanks

paskuale | Reply

May 24. 2008 19:23

Thanks for saving me a lot of my usual error-and-trial and headbanging trying to get something to work cross-browser!

cubiclegrrl | Reply

June 9. 2008 18:46

I was always getting incomplete forms and finally came up with an idea to put an end to that. My solution to this problem was to add one more step prior to the "submit form" button. I added a drop-down box proceded by "My form is.....". The top line on the box says "not ready to submit". The drop down says "ready to submit". I made this box be a required step and have not had one incomplete form since. If someone accidentally hits enter, they get an error message and are directed to the "my form is" line. They can simply scroll back to the line they were on and finish entering their information.

admins | Reply

July 8. 2008 00:46

A non-javascript method:

1.  Add a submit button at the top of the form.
2.  Render it off-screen with style="position: absolute; left: -1000px"  or such.  Don't use "display: none" or "visibility: hidden" because IE will ignore the submit button in those cases.

So, when the use types the enter key, the page posts back but no unintended events should fire.

Name | Reply

December 9. 2008 14:43

Thanks,Its saved my time Smile

Pradeep | Reply

January 6. 2009 17:27

Hey, thanks alot. it is working greatly.

prashanth | Reply

February 20. 2009 10:41

good solution

coolpositive | Reply

March 4. 2009 05:53

Hey man, thanks that worked great! Love your code! SmileSmile

G | Reply

March 4. 2009 12:42

Thanks Guys,

Working good for me.

Salman | Reply

September 10. 2009 17:05

Thnx! Only I have one question: Why use return in the onkeypress event?

Martijn | Reply

September 10. 2009 17:50

Hi Martijn,

In order to disable form to submit when enter key is pressed, you have to cancel the event by returning false. For other keys (letters and numbers for example), function returns true and event fires.

Blogging Developer | Reply

September 26. 2009 08:11

This worked great! Thanks so very much!

theFun | Reply

September 30. 2009 19:45

Thanks!  Any attribution needed?

Trung | Reply

October 10. 2009 19:52

Thank for this post; it was of great help to me.  I like your site design as well.

Kourtesy | Reply

October 10. 2009 19:56

Thanks for this post; it was of great help to me. I've only tried it in IE, and it worked well in IE.  I like your site design also (good colors, pretty crispy).

Kourtesy | Reply

October 14. 2009 18:23

Thanks a lot! Just in time!!!

rapid4me | Reply

October 17. 2009 00:57

Thank you, was helpful to solve my problem.

Keep going!!

Srinivas Komma | Reply

October 22. 2009 02:11

Very Nice!  Clean quick efficient code! Needed this glad Google found you..

PHP Newbie | Reply

October 22. 2009 12:56

Thanks, man!

bcode | Reply

October 22. 2009 17:30

Social comments and analytics for this post

This post was mentioned on Twitter by bloggingdev: How To: Disable Form Submit on Enter Key Press - http://su.pr/7HhK3H

uberVU - social comments | Reply

November 11. 2009 01:22

I always wanted to write in my site something like that but I guess you'r faster Smile

cash advance | Reply

November 16. 2009 15:20

This would be solved by proper validation of fields prior to form submission.

Dean | Reply

Add comment




(Will not be displayed!)