Date Added: 14/01/2015

<script language="javascript" type="text/JavaScript"">

document.onkeypress = keyPress;
var count = 1;

function keyPress() {
if (window.event.keyCode == 13)
{
 event.returnValue=false;
 event.cancel = true;
}

}
</script>
OR
<body OnKeyPress="return disableKeyPress(event)">
<script language="JavaScript">
function disableEnterKey(e)
{
 var key;
 if(window.event)
 key = window.event.keyCode; //IE
 else
 key = e.which; //firefox

return (key != 13);
}
modified by muslum21 for jquery
$("body").keydown(function (e) {

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

return (key != 13);

});

</script>

Here is a modified version of my function. It does the following: Prevents the enter key from working on any element of the form other than the textarea, button, submit. The enter key now acts like a tab. preventDefault(), stopPropagation() being invoked on the element is fine, but invoked on the form seems to stop the event from ever getting to the element. So my workaround is to check the element type, if the type is not a textarea (enters permitted), or button/submit (enter = click) then we just tab to the next thing. Invoking .next() on the element is not useful because the other elements might not be simple siblings, however since DOM pretty much garantees order when selecting so all is well.

function preventEnterSubmit(e) {
 if (e.which == 13) {
 var $targ = $(e.target);

if (!$targ.is("textarea") && !$targ.is(":button,:submit")) {
 var focusNext = false;
 $(this).find(":input:visible:not([disabled],[readonly]), a").each(function(){
 if (this === e.target) {
 focusNext = true;
 }
 else if (focusNext){
 $(this).focus();
 return false;
 }
 });

return false;
 }
 }
}

&nbsp;

http://stackoverflow.com/questions/1563062/prevent-form-submission-with-enter-key

Last Update: Posted by: müslüm ÇEN
Not Commented Yet !
Please login in order to comment . Login