Chris Pinski Chris Pinski - 6 months ago 11
HTML Question

Disabling enter key for form

I have been trying to disable the Enter key on my form. The code that I have is shown below. For some reason the enter key is still triggering the submit. The code is in my head section and seems to be correct from other sources.

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

return (key != 13);
},

Answer

if you use jQuery, its quite smiple. Here you go

$(document).keypress(
    function(event){
     if (event.which == '13') {
        event.preventDefault();
      }


});
Comments