Monday, February 21, 2005

Disabling Enter Key (JavaScript,ASP.NET)

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~>~
Note: Take off the ~ (cant put HTML tags here.. courtesy of google)

There you go, enter key disabled. :)

Comments:
_
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?

© Copyright 2005, Rohan Thomas
Disclaimer
This Blog contains findings that are self-explored and expressions that are self-evoked. They are by no meanss representions of my employer's view.