Asp.Net : Disable and Restrict Copy Cut and Paste from on a textbox

August 19, 2013

 

You will need two javascript functions for this:

function noCopyMouse(e) {
var isRight = (e.button) ? (e.button == 2) : (e.which == 3);

if(isRight) {
alert(‘You are prompted to type this twice for a reason!’);
return false;
}
return true;
}
function noCopyKey(e) {
var forbiddenKeys = new Array(‘c’,’x’,’v’);
var keyCode = (e.keyCode) ? e.keyCode : e.which;
var isCtrl;
if(window.event)
isCtrl = e.ctrlKey
else
isCtrl = (window.Event) ? ((e.modifiers & Event.CTRL_MASK) == Event.CTRL_MASK) : false;

if(isCtrl) {
for(i = 0; i < forbiddenKeys.length; i++) {
if(forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) {
alert(‘You are prompted to type this twice for a reason!’);
return false;
}
}
}
return true;
}

Handle the two events for the textbox(es) in codebehind ( in pageload event))

if(!isPostBack)
{
Textbox1.Attributes.Add(“onmousedown”, “return noCopyMouse(event);”)
Textbox1.Attributes.Add(“onkeydown”, “return noCopyKey(event);”)
}