Wednesday, December 8, 2010

Disable Cut, Copy and Paste function for textbox using jQuery

Today my client asked me to disable cut, copy and paste functionality for the Textbox, which means User should not be allowed cut or copy text from the Textbox and also to paste text within Textbox. Well, it is very easy to do this using jQuery. All you need is to bind cut, copy and paste function to Textbox and stop the current action. See below code.
$(document).ready(function(){
  $('#txtInput').live("cut copy paste",function(e) {
      e.preventDefault();
  });
});
You can also use bind function to bind these events. Read more about bind() method here.
$(document).ready(function(){
  $('#txtInput').bind("cut copy paste",function(e) {
      e.preventDefault();
  });
});
But it is suggested to use live() method. Read more about live() method here and read this article to find out the difference between bind() and live().
See live Demo and Code.
Feel free to contact me for any help related to jQuery. I will gladly help you.