posted on Thursday, August 25, 2005 10:00 PM
by
rohanthomas
Disabling Enter Key (JavaScript,ASP.NET) [Ported Blog Entry]
Porting old blog's post from my previous blog here -> http://xtremedotnet.blogdrive.com/
Original Date Of Posting : 19-Feb-2005
Just a Question I came across browing one of my forums;
Q. How do I disable the enter key?
A. Well its just a piece of code which goes like this :
function modifKey(e)
{
var modif='';
if (event.altKey)
{
modif+='[ Alt ] ';
}
if (event.ctrlKey)
{
modif+='[ Ctrl ] ';
}
if (event.shiftKey)
{
modif+='[ Shift ] ';
}
getKey(e,modif);
}
function getKey(e,modif)
{
if (e.keyCode)
{
keycode=e.keyCode;
}
else
{
keycode=e.which;
}
char=String.fromCharCode(keycode);
xCode=char.charCodeAt(0);
if (xCode == 13) //Enter key is pressed!!
{
window.event.keyCode = 0;
}
else
{
return true;
}
}
So as you can see, we are trying to catch the enter key and return true if any key except the enter key is pressed!
Since we want this to work for the whole page, put this is the body tag of the page :
<body MS_POSITIONING="GridLayout" onkeypress=' return modifKey(event);'>
</body>
There you go, enter key disabled. :)