Hi all,
I am using the following code to disable the ctrl+a/c/v/x keys.
It's working fine, but the problem is that it disables the keys for the complete document wherever I want it only for the particular text field.
Also one point to note is that I can't use the following to achieve this
Is it possible to use the onkeypress and onkeydown in the script itself in place of using it on a particular field so that it will disable the key for that particular field.
Thanks
Gaurav Jain
I am using the following code to disable the ctrl+a/c/v/x keys.
Code:
<html>
<head>
<script language="JavaScript" type="text/javascript">
function disableCtrlKeyCombination(e)
{
//list all CTRL + key combinations you want to disable
var forbiddenKeys = new Array('a', 'c', 'x', 'v');
var key;
var isCtrl;
if(window.event)
{
key = window.event.keyCode;//IE
if(window.event.ctrlKey)
isCtrl =true;
else
isCtrl =false;
}
else
{
key = e.which; //firefox
if(e.ctrlKey)
isCtrl = true;
else
isCtrl = false;
}
//if ctrl is pressed check if other key is in forbidenKeys array
if(isCtrl)
{
for(i=0; i< forbiddenKeys.length; i++)
{
//case-insensitive comparation
if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
{
return false;
}
}
}
return true;
}
document.onkeypress=disableCtrlKeyCombination
document.onkeydown=disableCtrlKeyCombination
</script>
<title>Untitled Page</title>
</head>
<body>
<form id="form1">
<input type="text" name="mytext" id="txtinput" />
<input type="text" name="test" />
</form>
</body>
</html>
Also one point to note is that I can't use the following to achieve this
Code:
<input type="text" name="mytext" onkeypress="return disableCtrlKeyCombination(event);" onkeydown="return disableCtrlKeyCombination(event);" />
Thanks
Gaurav Jain
Comment