Naveen P.N

Just another Blog to play with programming

Disabling Copy Paste in a TextBox

leave a comment »

Hi this article is more or less similar to the last article “Restricting the mouse click on a web page” the logic is the same with little intelligence. In this article I will disable the Copy, Paste and Ctrl Functionality of a TextBox, before reading this article I request you to please check the previous article for better understanding.

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Untitled Page</title>
<script type=”text/javascript” src=”Restrict Copy and Paste.js”>
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<pre>
Right Click Disabled   : <asp:TextBox ID=”TextBoxCopy” runat=”server”
onMouseDown=”DisableRightClick(event)”>
</asp:TextBox>
Ctrl Key Disabled      : <asp:TextBox ID=”TextBox2″ runat=”server”
onKeyDown=”return DisableCtrlKey(event)”>
</asp:TextBox><br />
Copy and Paste Disabled: <asp:TextBox ID=”TextBox1″ runat=”server”
oncopy=”return false” onpaste=”return false”                    oncut=”return false”>       </asp:TextBox>
</pre>
</form>
</body>
</html>

Restrict Copy and Paste.aspx

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head runat=”server”>

<title>Untitled Page</title>

<script type=”text/javascript” src=”Restrict Copy and Paste.js”>

</script>

</head>

<body>

<form id=”form1″ runat=”server”>

<pre>

Right Click Disabled   : <asp:TextBox ID=”TextBoxCopy” runat=”server”

onMouseDown=”DisableRightClick(event)”>

</asp:TextBox>

Ctrl Key Disabled      : <asp:TextBox ID=”TextBox2″ runat=”server”

onKeyDown=”return DisableCtrlKey(event)”>

</asp:TextBox><br />

Copy and Paste Disabled: <asp:TextBox ID=”TextBox1″ runat=”server”

oncopy=”return false” onpaste=”return false” oncut=”return false”> </asp:TextBox>

</pre>

</form>

</body>

var buttonright = 2; // button right

function DisableRightClick(event)

{

//For mouse right click

if (event.button==buttonright)

{

alert(“Right Clicking not allowed!”);

}

}

</html>

Restrict Copy and Paste.js

function DisableCtrlKey(e)

{

var code = (document.all) ? event.keyCode:e.which;

var message = “Ctrl key functionality is disabled!”;// look for CTRL key press

if (parseInt(code)==17)

{

alert(message);

window.event.returnValue = false;

}

}


text1


text2


Download Code with Article

Written by Naveen P.N

October 8, 2009 at 11:19 pm

Posted in ASP.NET

Leave a Reply