Prevent cut, copy paste on textbox using jQuery


  • In our development process, we come across many interesting and starnge scenarios.
  • On of them is not allow user to copy, or cut text from textbox and paste into the same.
  • This scenario can be achieved easily using jQuery.
  • We need to bind the copy, cut and paste methods to textbox which are provided by jQuery and stop or prevent the default action.

Watch Live Demo

Code :
$(document).ready(function () {
    $("#start").live("cut copy paste", function (e) {
        e.preventDefault();
        alert("The operation '" + e.type + "' is not allowed !");
    });
});

Demo :




In the above example, we are binding the cut, copy and paste functions to the textbox and preventing the default behaviour. When user tries these operations, the default behaviour is prevented.

We can also use the bind method in place of live method.


Code :

$(document).ready(function () {
    $("#start").bind("cut copy paste", function (e) {
        return false;
    });
});

Demo :



In the above example, we have used bind method, and used return false to prevent cut, copy and paste operations.


Thus jQuery makes it pretty easy to disable cut copy and paste operations on a textbox.


0 comments:

Post a Comment