Wednesday, July 25, 2012

Disable copy paste right click using javascript in asp.net TextBox.

This blog defines How to Disable copy paste and right click using javascript in asp.net TextBox. We make a function in JavaScript to disable right click. When we create right click on the textbox message will be generate to show disable right click.


we can achieve this in 2 ways

1. use this method when u 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 Pagetitle>
<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 textbox
 strong> <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>

I hope this could be useful for you.

Thanks.

Prevent Cut, Copy and Paste Operations in an ASP.NET TextBox using jQuery

In this article, I will demonstrate how to prevent users from doing Cut, Copy and Paste operations in an ASP.NET TextBox using jQuery.

The event handling approach in jQuery begins by attaching a handler to an event. The jQuery bind() event method does exactly the same and binds one or more events to a handler. The signature of the bind() method is as follows:

bind(eventType,data,handler)
The parameters of the bind() method are as follows:
eventType is a string holding a JavaScript event type such as click, focus, keyup etc.
data is some optional data you want to pass to the handler
handler is the event handler function which will execute when the event is triggered
Let us quickly jump to the solution and see how we can use the bind() event method to write minimal code to prevent cut, copy and paste operations on the textbox.
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>Prevent Cut, Copy and Paste Operations in a TextBox title>
    <script type='text/javascript' src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'>   
    </script>
    <script type="text/javascript">
     $(function() {
     $('input[id$=tb1]').bind('cut copy paste', function(e) {
     e.preventDefault();
     alert('You cannot ' + e.type + ' text!');
      });
    });
   
    script>
    <body>
    <form id="form1" runat="server">
    <div class="bigDiv">
    <h2>Prevent Cut, Copy and Paste Operations in a TextBox

    :TextBox ID="tb1" runat="server" Text="Text which cannot be copied/cut/pasted"
    ToolTip="Try Copy/Cut/Paste in textbox"/>

   


   
   
Observe how convenient it is to use jQuery to list multiple events (cut, copy, paste) together and bind it to a handler as shown below.
$('input[id$=tb1]').bind('cut copy paste', function(e) {});
If the user performs any of these events on the textbox (tb1), the default behavior is prevented using e.preventDefault() and the user is alerted with a message. The e.type describes the type of event performed.
$('input[id$=tb1]').bind('cut copy paste', function(e) {
e.preventDefault();
alert('You cannot ' + e.type + ' text!');
});

CutCopyPaste
This piece of ‘minimal’ code handles a typical requirement where you are asked to ‘confirm’ an email address and want the user to type it manually, instead of copying and pasting it.
Note: Text can be edited in this textbox, but cut/copy/paste actions are prevented
This demo has been tested on IE7, IE8, Firefox 3, Chrome 2 and Safari 4.
I hope you liked this article and I thank you for viewing it.