Disable Cut, Copy, and Paste on Browser using JavaScript


In one of my banking applications, we have a requirement to disable the Ctrl+x, Ctrl+c, and Ctrl+v options on browser. That means we have a requirement to restrict the user to perform Cut, Copy, and Paste operations on the browser for security reasons. We can achieve this by using JavaScript because browser can understand JavaScript language only.

In this article we discuss how to disable Cut, Copy, and Paste options using JavaScript and Asp.Net. If you want you can try this example with simple HTML page also. Open Microsoft Visual Studio 2015 => Create Web Site. Change Default.aspx page content as shown below.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Disable Ctrl+x, Ctrl+c, and Ctrl+v</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

       <input type="text" id="text1" />

        <label>This is example for disabling the Cut, Copy, and Paste options using JavaScript</label>

 

        <script type="text/javascript">

   

    var message = "Due to security reasons, Right Click is not allowed.";

 

    function clickIE4() {

        if (event.button == 2) {

            alert(message);

            return false;

        }

    }

 

    function clickNS4(e) {

        if (document.layers || document.getElementById && !document.all) {

            if (e.which == 2 || e.which == 3) {

                alert(message);

                return false;

            }

        }

    }

    if (document.layers) {

        document.captureEvents(Event.MOUSEDOWN);

        document.onmousedown = clickNS4;

    }

    else if (document.all && !document.getElementById) {

        document.onmousedown = clickIE4;

    }

 

    document.oncontextmenu = new Function("alert(message);return false")

   

    //Disable Copy into HTML form using Javascript

    document.body.oncut=new Function("return false"); 

 

    //Disable Copy into HTML form using Javascript

    document.body.oncopy=new Function("return false");

 

    //Disable Paste into HTML form using Javascript 

    document.body.onpaste=new Function("return false");

 

</script>

    </div>

    </form>

</body>

</html>

 

As shown above add JavaScript code inside the body tag. Here we are disabling the Cut, Copy, and Paste options by using document.body oncut, oncopy, and onpaste properties. The Defalt.aspx page contains one label with some text and one text box. Run the application and try to perform Cut, Copy, and Paste actions on text box or label by using right click or by using Ctrl+x, Ctrl+c, and Ctrl+v. When we try to do any of these operations it does not perform it. If you right click on the browser it displays the warning message as “Due to security reasons, Right Click is not allowed”.