Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent copy/paste and right-click meant for textbox (email address)

I would like to prevent a user from either:

  1. Copy and pasting from the first textbox to the second
  2. Right-click and use the context menu to copy and paste from the first textbox to the second.

This is not working.

<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>

GOT IT WORKING!!!!

<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>
like image 547
Susan Avatar asked Mar 15 '12 17:03

Susan


People also ask

How do I restrict copy and paste in a TextBox?

Disable Copy and Paste in an ASP.NET Webpage TextBox without JavaScript; we need to set the following properties of the TextBox: oncopy="return false" onpaste="return false" oncut="return false"

How do I enable copy paste in TextBox?

Use KeyPress, KeyUp, KeyDown for the same... <asp:TextBox onkeypress="return false;" onkeyup="return false;" onkeydown="return false;" ... You can use javascript key events for this. Use KeyPress, KeyUp, KeyDown for the same...

How do I restrict copy and paste in HTML?

You can use jquery for this: $('body'). bind('copy paste',function(e) { e. preventDefault(); return false; });

How do I turn off copy and paste?

To disable the copy and paste function in PDFs, you need to prevent the ability to select text with the cursor. If a user can't select the text, then they can't right-click and copy with the mouse or use Ctrl-C as a keyboard command.


1 Answers

When using jQuery this is fairly simple and it is 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 that explains how this works together with ASP.NET:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=398

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

UPDATE
Since you asked to prevent the context menu entirely 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 shown when false is returned. Here is an overview of browsersupport for this:
http://help.dottoro.com/ljhwjsss.php

like image 142
ntziolis Avatar answered Sep 21 '22 11:09

ntziolis