Disabling cut, copy, paste, right click and context menu in a textbox on aspx page

In this tutorial you will learn how to disable cut, copy, paste, right click and context menu in asp:textbox. Sometimes due to security point of view you may want to do this. It's quite simple, I often see that developers write javascript code to disable the cut, copy, paste in textbox either textbox is input or server side asp:textbox.


I will not use any single line of javascript code to achieve what I want. Now let's have a look over how to do so.

Disabling cut, copy, paste, right click and context menu in a textbox


<asp:TextBox ID="txtName" runat="server" oncopy="return false" onpaste="return false" oncut="return false" oncontextmenu="return false">
</asp:TextBox>
You can use same concept with input textbox like

<input type="text" ID="txtName" oncopy="return false" onpaste="return false" oncut="return false" oncontextmenu="return false"/>

to disable the copy, paste, cut, right click and contextmenu in textbox.
I have used the events (oncopy, onpaste, oncut, oncontextmenu) of textbox to achieve the desired disability.

If you want to show the alerts then you have to modify the code like this

<input type="text" ID="txtName" oncopy="disableCopy();" onpaste="disablePaste();" oncut="disableCut();" oncontextmenu="disableContextMenu();"/>


<script language="javascript" type="text/javascript">
function disableCopy()
{
alert("You cannot perform Copy");
return false;
}


function disablePaste()
{
alert("You cannot perform Paste");
return false;
}


function disableCut()
{
alert("You cannot perform Cut");
return false;
}


function disableContextMenu()
{
alert("You cannot perform right click via mouse as well as keyboard");
return false;
}
</script>

I hope you will find this article very interesting.
Happy Coding Keep Coding

0 comments: