The Original Jelani Harris The one and only


Using Jquery to disable the enter key

There are times that you do not want a form to automatically submit when a user hits the enter key. Or if you want to do some validation via javascript before you allow the submit to go through.

//Bind this keypress function to all of the input tags
$("input").keypress(function (evt) {
//Deterime where our character code is coming from within the event
var charCode = evt.charCode || evt.keyCode;
if (charCode  == 13) { //Enter key's keycode
return false;

By returning false in the keypress function it tells the browser not to allow the enter key event.

Comments (12) Trackbacks (0)
  1. Just what I was looking for. Thank you!

  2. Thanks, that was driving me nuts trying to solve.

  3. Thank you, i searching for a long time for this.

  4. nice => evt.charCode || evt.keyCode;
    no other snippets were covering cross convention <3

  5. The easiest way to solve this kind of issue ever!!
    Great job!

  6. And how to enable it again?

  7. Never heard of spacing

  8. This is not working for me in firefox for some reason. Any ideas? I am using JQ 144. Is that too old for this solution?

  9. @Tanner : maybe there is incorrect on your script , i am using 1.3 Jq and its works

  10. thank u, i am looking for this

  11. Thanks you very much !

  12. Thanks! I tried a lot of other approaches, but they did not work in Firefox’.
    Yours does!

Leave a comment

No trackbacks yet.