Go to DotNetFunda.com
 Online : 1763 |  Welcome, Guest!   Login
 
Home > Articles > JavaScript > Disabling copy paste in a textbox on aspx page

  • Download the OOPS, ASP.NET and ADO.NET Training Videos for FREE, click here.

Submit Article | Articles Home | Search Articles |

Disabling copy paste in a textbox on aspx page

 Posted on: 1/22/2009 8:21:06 AM by Amit.jain | Views: 4290 | Category: JavaScript | Level: Intermediate | Print Article
For many reasons sometime we don't want to allow user to use right click to copy paste or by using ctrl+C , ctrl+v to copy and paste in textbox on a aspx page in asp.net

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

Introduction
We can use javascript to disable right mouse click or ctrl keys to ensure user is not able to copy paste in a textbox
we can achieve this in 2 ways

1. use this method when you don't want any alerts or message

<asp:TextBox ID="TextBox1" runat="server"
oncopy="return false"
onpaste="return false"
oncut="return false">
</asp:TextBox>
2. If you want to show alerts than use this method instead 
Right this javascript function in the head section of aspx page, in this function we are disabling right mouse click and ctrl keys
<head runat="server">
<title>Untitled Page</title>
<script language="javascript">
function DisableRightClick(event)
{
//For mouse right click
if (event.button==2)
{
alert("Right Clicking not allowed!");
}
}
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;
}
}
</script>
</head>
Now use this function on the textbox which we want to disable copy paste and right clicking
<body>
<form id="form1" runat="server">
<div>
<strong>
Right click disabled</strong> textbox
<br />
<asp:TextBox ID="TextBoxCopy" runat="server"
onMouseDown="DisableRightClick(event)">
</asp:TextBox><br />
<br />
<strong>Ctrl key </strong>disabled<br />
<asp:TextBox ID="TextBox2" runat="server"
onKeyDown="return DisableCtrlKey(event)">
</asp:TextBox><br />
<br />

Another method to disable<strong> Cut,Copy and paste
</strong>in textbox<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"
oncopy="return false"
onpaste="return false"
oncut="return false">
</asp:TextBox>
</form>
</body>
Hope this helps 


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


About amiT jaiN

Experience:4 year(s)
Home page:http://csharpdotnetfreak.blogspot.com/
Member since:Friday, December 26, 2008
Level:Starter
Status: [Member]
Biography:
 Latest post(s) from Amit.jain

   ◘ Disabling copy paste in a textbox on aspx page posted on 1/22/2009 8:21:06 AM
   ◘ Creating AutoComplete TextBox in Windows Froms using C# posted on 1/13/2009 6:45:30 AM
   ◘ Implementing autocomplete textbox in gridview using Ajax autocomplete extender posted on 1/11/2009 10:05:43 AM
   ◘ Implementing search in GridView and highlight results posted on 1/8/2009 5:22:39 AM
   ◘ Redirection to Login page after session time out posted on 1/5/2009 7:49:14 AM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)