In this article I will explain with an example, how to disable or prevent CUT, COPY and PASTE operations in HTML TextBox, HTML TextArea and ASP.Net TextBox using JavaScript.
Cut, Copy and Paste operations in TextBox or TextArea can be performed using CTRL button or on Mouse Right Click.
This article will illustrate how to disable Cut, Copy and Paste operations using both CTRL button and Mouse Right Click.
 
 
Disable Cut Copy and Paste in TextBox and TextArea using JavaScript
The following HTML Markup consists of HTML TextBox, HTML TextArea and ASP.Net TextBox which are assigned with CssClass disabled.
Inside the window onload event handler, all the elements having the Css Class disabled are referenced and for each element, Cut, Copy and Paste event handlers are attached using JavaScript.
Inside the Cut, Copy and Paste event handlers, the event is cancelled using the preventDefault function.
Now whenever user tries to perform Cut, Copy and Paste operations, the operation is not performed.
The best part of this snippet it not only disables the Cut, Copy and Paste operations with CTRL button but also disables it on Mouse Right Click without disabling Right Click functionality.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            var controls = document.getElementsByTagName("*");
            var regEx = new RegExp("(^| )disable( |$)");
            for (var i = 0; i < controls.length; i++) {
                if (regEx.test(controls[i].className)) {
                    AttachEvent(controls[i], "copy");
                    AttachEvent(controls[i], "paste");
                    AttachEvent(controls[i], "cut");
                }
            }
        };
        function AttachEvent(control, eventName) {
            if (control.addEventListener) {
                control.addEventListener(eventName, function (e) { e.preventDefault(); }, false);
            } else if (control.attachEvent) {
                control.attachEvent('on' + eventName, function () { return false; });
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    ASP.Net<br /><br />
    <asp:TextBox ID="TextBox2" runat="server" CssClass="disable"></asp:TextBox><br /><br />
    <asp:TextBox ID="TextBox1" runat="server" TextMode = "MultiLine" CssClass="disable"></asp:TextBox><br /><br /><br />
    HTML<br /><br />
    <input type = "text" class = "disable" /><br /><br />
    <textarea class = "disable" rows = "5" cols = "5"></textarea>
    </form>
</body>
</html>
 
 
Screenshot
Disable Cut Copy and Paste and in TextBox and TextArea using JavaScript
 
 
Browser Compatibility
The above code has been tested in the following browsers.
Internet Explorer  FireFox  Chrome  Safari  Opera 
* All browser logos displayed above are property of their respective owners.

 

 
 
Demo
 
 
Downloads