Disable Copy, Paste, and Cut in Input Field Using JavaScript
How to prevent users from using copy, paste, and cut operations inside input fields for better control and data integrity.
This page demonstrates how to prevent users from copying, pasting, or cutting text in an input field using JavaScript. This can be useful in scenarios where data input should only be done manually to ensure integrity or security.
Contents
Live Demo: Input Field with Copy, Cut, and Paste Disabled
Test the input below you won't be able to copy, cut, or paste anything. This showcases JavaScript preventing those actions.
const input = document.getElementById("secureInput");
["copy", "paste", "cut"].forEach(eventType => {
input.addEventListener(eventType, function(e) {
e.preventDefault();
alert(eventType + " is disabled in this field.");
});
});
Disable Copy in Input Field
This input field prevents users from copying text using JavaScript.
document.getElementById("disable-copy").addEventListener("copy", function(e) {
e.preventDefault();
alert("Copy is disabled in this field.");
});
Disable Paste in Input Field
This input field blocks all paste actions to ensure manual data entry.
document.getElementById("disable-paste").addEventListener("paste", function(e) {
e.preventDefault();
alert("Paste is disabled in this field.");
});
Disable Cut in Input Field
This input field disables the cut operation using a simple JavaScript event.
document.getElementById("disable-cut").addEventListener("cut", function(e) {
e.preventDefault();
alert("Cut is disabled in this field.");
});
Disable Text Drop in Input Field
This input field prevents users from dragging and dropping text into it. This ensures that content must be typed manually, avoiding pasted or dragged text.
document.getElementById("no-drop-input").addEventListener("drop", function(e) {
e.preventDefault();
alert("Dropping text is disabled in this input.");
});
Disable Copy, Paste, Right-Click, Selection, and Drag and Drop in Input Field
This reusable JavaScript function disables multiple user actions such as copy, paste, cut, right-click (context menu), text selection and drag and drop operations on an input field. It is useful for protecting sensitive inputs or enforcing manual data entry.
// Prevent multiple events at once
const disableAllActions = (input) => {
const events = ['copy', 'paste', 'cut', 'contextmenu', 'selectstart', 'dragstart', 'drop'];
events.forEach(event => {
input.addEventListener(event, (e) => {
e.preventDefault();
return false;
});
});
};
// usage
const input = document.getElementById("disable-all");
disableAllActions(input);