Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent user from copying the text using jQuery?

how do i prevent the user from Copying any text in asp.net page using jQuery?

like image 204
HAJJAJ Avatar asked Apr 17 '11 07:04

HAJJAJ


People also ask

How do I restrict copy and paste in jQuery?

To disable cut, copy and paste of a content in jQuery, use the jQuery bind() function.

How do I restrict copy and paste in text field?

The event. preventDefault() method stops the default action of an element from happening. You can use this method toprevent a TextBox control from Cut (Ctrl+X) , Copy (Ctrl+C) and Paste (Ctrl+V) .

How do I disable copy paste in 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 turn off copy and paste?

That means CTRL + C should be disabled and CTRL + A should be enabled.


2 Answers

You can disable the right click and also bind keyup event on document to detect copy command key combination "Ctl + C" and returning false.

To disable right click:

jQuery(document).bind("contextmenu", function(e) {
 e.preventDefault();
});

To detect Ctl + C:

jQuery(document).ready(function()
{
    var ctlPressed = false; //Flag to check if pressed the CTL key
    var ctl = 17; //Key code for Ctl Key
    var c = 67; //Key code for "c" key

    jQuery(document).keydown(function(e)
    {
        if (e.keyCode == ctl) 
          ctlPressed = true;
    }).keyup(function(e)
    {
        if (e.keyCode == ctl) 
          ctlPressed = false;
    });

    jQuery(".your-no-copy-area").keydown(function(e)
    {
        if (ctlPressed && e.keyCode == c) 
          return false;
    });
});
like image 187
Naveed Ahmad Avatar answered Sep 23 '22 20:09

Naveed Ahmad


Watermark is your solution. I can easily disable Javascript.

like image 38
Grayson Peddie Avatar answered Sep 24 '22 20:09

Grayson Peddie