Prevent copy / paste and right-click for text field (email address)

I would like to ban the user:

  • Copy and paste from the first text field to the second
  • Right-click and use the context menu to copy and paste from the first text field to the second.

This does not work.

<html> <head runat="server"> <title>Confirm email page</title> <script type="text/javascript" language="javascript"> function DisableRightClick(event) { //For mouse right click if (event.button == 2) { } } function DisableCtrlKey(e) { var code = (document.all) ? event.keyCode : e.which; // look for CTRL key press if (parseInt(code) == 17) { window.event.returnValue = false; } } </script> </head> <body style="font-family: Verdana; font-size: 1em"> <form id="form1" runat="server"> <div> <h1>Confirm Email</h1> <asp:Label ID="Label2" runat="server" Text="Enter Email Address: "></asp:Label> <asp:TextBox ID="TextBox2" runat="server" oncopy="return false" onMouseDown="DisableRightClick(event)" ></asp:TextBox><br /> <asp:Label ID="Label3" runat="server" Text="Confirm Email Address: "></asp:Label> <asp:TextBox ID="TextBox3" runat="server" onKeyDown="return DisableCtrlKey(event)"></asp:TextBox><br /> </div> </form> </body> </html> 

GET THIS WORK !!!!

 <div> <h1>Copy Paste Preventer!!!</h1> <asp:Label ID="Label1" runat="server" Text="Enter Username: "></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <asp:Label ID="Label2" runat="server" Text="Enter Email Address: "></asp:Label> <asp:TextBox ID="email" runat="server" oncopy="return false" onpaste="return false" oncut="return false" oncontextmenu="forms[0].elements[0].value++; return false"></asp:TextBox><br /> <asp:Label ID="Label3" runat="server" Text="Confirm Email Address: "></asp:Label> <asp:TextBox ID="TextBox3" runat="server" oncopy="return false" onpaste="return false" oncut="return false" oncontextmenu="forms[0].elements[0].value++; return false"> </asp:TextBox><br /> </div> 
+7
source share
2 answers
 <div> <h1>Copy Paste Preventer!!!</h1> <asp:Label ID="Label1" runat="server" Text="Enter Username: "></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <asp:Label ID="Label2" runat="server" Text="Enter Email Address: "></asp:Label> <asp:TextBox ID="email" runat="server" oncopy="return false" onpaste="return false" oncut="return false" oncontextmenu="forms[0].elements[0].value++; return false"></asp:TextBox><br /> <asp:Label ID="Label3" runat="server" Text="Confirm Email Address: "></asp:Label> <asp:TextBox ID="TextBox3" runat="server" oncopy="return false" onpaste="return false" oncut="return false" oncontextmenu="forms[0].elements[0].value++; return false"> </asp:TextBox><br /> </div> 
+2
source

When using jQuery, it is quite simple and fully compatible with ASP.NET:

 <script type='text/javascript' src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'> </script> <script type="text/javascript"> $(function() { $('input[id$=TextBox2]').bind('cut copy paste', function(e) { e.preventDefault(); alert('You cannot ' + e.type + ' text!'); }); }); </script> 

Here is an article explaining how this works with ASP.NET:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=398

As Scott pointed out: during production, you should put a jQuery link on the bottom of your html (still inside the body tag).

UPDATE
Since you asked to completely exclude the context menu, you can do something like this:

Add this script:

 <script type="text/javascript"> document.getElementById('TextBox2').oncontextmenu = function (){ return false; }; </script> 

The menu item is not displayed when false returned. Here is a browser overview for this:
http://help.dottoro.com/ljhwjsss.php

+4
source

All Articles