The Wayback Machine - https://web.archive.org/web/20240712104133/https://www.geeksforgeeks.org/create-a-disable-right-click-copy-cut-paste-using-javascript/
Open In App

Create a Disable Right Click, Copy, Cut & Paste using JavaScript

Last Updated : 13 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we have created a simple user interface using HTML and CSS, where there is an input field on which Right-Click, Copy, Cut, and Paste counts are displayed. When the user tries to perform these operations, a warning or error message is displayed, and the counter value is incremented for each operation. We can use this application for the input fields that are highly concerned with security, like passwords, OTP, etc. where the user cannot paste the passwords from other sources and also cannot view the page source code.

Preview Image

Screenshot-2023-10-12-150718

Prerequisites

Approach

  • Firstly, we need to create the application structure using HTML tags like <div>, <h1> for the heading (GeeksforGeeks Disable Application>, <input> that will take the input from the user. <p> to display the count values when the user performs the operations.
  • Then, we will create the styling file for the basic styling of our application, like the alignment of the components, padding of fields, color structure, and scheme of the elements.
  • In the main JavaScript file, firstly, we are getting references to various HTML elements by using their IDs like a message (this is the element for showing the error message).
  • Then we initialize the values of all the counts with 0.
  • Now, by using the event listeners, we are setting up the ‘context menu’ for operations of right-click, copy, and paste on the inpBox element. For example, when the right-click event occurs, it prevents displaying the browser’s default context menu, and simultaneously, the value of the counter is also incremented.
  • The function for showing the error message is created that passes the argument in the ‘errorMessage’ element. Using the setTimeOut function, the message disappeared after 2 seconds.

Example: This example shows the use of the above-explained approach.

HTML




<!-- HTML FILE -->
<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>
        GeeksforGeeks Disable Application
    </title>
    <link rel="stylesheet" href="./style.css">
</head>
  
<body>
    <div class="container">
        <h1>
            GeeksforGeeks Disable Application
        </h1>
        <input class="input-box" 
               type="text" 
               id="myInput" 
               placeholder="Enter Text">
        <div class="warning-message" 
             id="message" 
             style="display: none;">
        </div>
        <p>Right-click Count:
            <span id="rightClickCounter">
              0
              </span>
        </p>
        <p>Copy Count:
            <span id="copyCounter">
              0
              </span>
        </p>
        <p>Paste Count:
            <span id="pasteCounter">
              0
              </span>
        </p>
    </div>
    <script src="./script.js"></script>
</body>
  
</html>


CSS




/* Style.css */
body {
    font-family: Arial, sans-serif;
    background-color: #f2f2f2;
}
  
.container {
    max-width: 400px;
    margin: 0 auto;
    padding: 20px;
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 0 0 10px
        rgba(0, 0, 0, 0.2);
    border-radius: 5px;
}
  
h1 {
    color: #00a63f;
    text-align: center;
    font-size: 24px;
}
  
.input-box {
    width: 80%;
    padding: 10px;
    font-size: 16px;
    border: 1px solid #ccc;
    border-radius: 5px;
    outline: none;
    display: block;
    margin: auto;
}
  
.warning-message {
    color: red;
    font-size: 14px;
    margin-top: 5px;
    text-align: center;
}


Javascript




// Script.js
  
document.addEventListener(
    "DOMContentLoaded",
    function () {
        let inpBox =
            document.getElementById(
                "myInput");
        let errorMessage =
            document.getElementById(
                "message");
        let rightClickCnt =
            document.getElementById(
                "rightClickCounter");
        let copyCounter =
            document.getElementById(
                "copyCounter");
        let pasteCounter =
            document.getElementById(
                "pasteCounter");
        let rightCnt = 0;
        let copyCnt = 0;
        let pasteCnt = 0;
  
        // Function For Right Count
        inpBox.addEventListener(
            "contextmenu",
            function (e) {
                e.preventDefault();
                rightCnt++;
                rightClickCnt.textContent =
                    rightCnt;
                showErrorMsg(
                    "Right-clicking is disabled."
                );}
        );
  
        // Function For Copy Count
        inpBox.addEventListener(
            "copy",
            function (e) {
                e.preventDefault();
                copyCnt++;
                copyCounter.textContent =
                    copyCnt;
                showErrorMsg(
                    "Copying is disabled."
                );}
        );
  
        // Function for Paste Count
        inpBox.addEventListener(
            "paste",
            function (e) {
                e.preventDefault();
                pasteCnt++;
                pasteCounter.textContent =
                    pasteCnt;
                showErrorMsg(
                    "Pasting is disabled."
                );}
        );
  
        // Function for Display Error Message
        function showErrorMsg(message) {
            errorMessage.innerText =
                message;
            errorMessage.style.display =
                "block";
  
            setTimeout(function () {
                errorMessage.style.display =
                    "none";
            }, 2000);}
    }
);


Output:
Animation



Similar Reads

How to disable Copy, Paste, Cut and Right Click using jQuery ?
The ctrl+c, ctrl+v, ctrl+x and a right-click of the mouse is used to copy, paste and cut anything from anywhere. It can be disable for particular task or page. Let see how to disable cut, copy, paste and right-click. It can be done in two ways: By using an on() method By using keydown() and mousedown() method By using an on() method: It is a built-
3 min read
How to Copy, Cut and Paste in Vi editor
Vi is a really strong text editor that you can use on Unix and Linux computers. It can do a lot of things, like copying, cutting, and pasting text. Even though these actions might seem hard at first, once you get the hang of them, they can make you much faster when you're working with text files. In this guide, we'll talk about how to copy, cut, an
6 min read
How to disable right click on web page using JavaScript ?
Disabling right-click on a web page can be done using the DOM Events. It is a client-side action, and it can be achieved using JavaScript. However, it's important to note that attempting to prevent right-clicking is not a foolproof method for protecting your content, as users can easily bypass it by disabling JavaScript or using browser features. T
2 min read
How to Disable Ctrl+V (Paste) in JavaScript?
What is Ctrl + V ?The ctrl+V is a keyboard shortcut used to paste anything from anywhere. It can be disabled for a particular task or page. Let's see how to disable cut, copy, paste, and right-click. To disable the ctrl+V (paste) keyboard shortcut in JavaScript, you would typically capture the keydown event and prevent the default behavior if the c
2 min read
How to detect copy paste commands Ctrl+V, Ctrl+C using JavaScript ?
In this article, we will detect copy-paste commands Ctrl+V, and Ctrl+C using JavaScript. To detect the combination of keys with "Ctrl", we use the ctrl property of the keydown event. ApproachEvent Listener: The script adds an keydown event listener to the document.Element Access: It accesses the HTML element with the id "myDiv" using document.getEl
2 min read
How to disable right-click option using the jQuery ?
The bind() method in jQuery is used to attach one or more event handlers for selected element and this method specifies a function to run when an event occurs. Syntax: $(selector).bind(event, data, function);Parameters: This method accepts three parameters as mentioned above and described below: event: It is an event type which is passed to the sel
2 min read
How to disable paste protection in Mozilla Firefox Developer Console?
All of us are very familiar with the browser named "Mozilla Firefox". So, you might have also come across an irritating bug that disables pastes into the developer console. I also faced the same problem. Most of you might have thought that the easiest way to get rid of this problem is to just simply switch to another browser but what if we can get
2 min read
How to Copy and Paste Text and Images in Google Docs
Google Docs is a free online document editor provided by Google. Google Docs makes it easier to create a document for personal and professional uses with various features. Some of the features in Google Docs are customizing font styles, font size, text alignment, and inserting media such as images, tables, word art, charts, sheets, and many more. T
8 min read
Difference between WebDriver click() and JavaScript click()
WebDriver offers a collection of APIs that can be used to communicate with web components, mimic user activities, and gather data from online pages. This article focuses on discussing the difference between WebDriver click() and JavaScript click(). We are Learning all the concepts related to the WebDriver click() and JavaScript click() in detail: T
6 min read
How to attach a click and double-click event to an element in jQuery ?
In this article, we will see how to attach a click and double-click event to an element in jQuery. To attach click and double-click events, click and dbclick events are used. These events are attached with the bind() method. This method is used to attach one or more event handlers for selected elements and this method specifies a function to run wh
1 min read
three90RightbarBannerImg