Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

How to disable copy paste in textbox using jQuery


Share with WhatsApp


Hello friend, in this post I am going to share simple way of disabling Cut, Copy and Paste operation in input textbox using jQuery. It prevents user to perform any of these operations including shortcuts like Ctrl+C, Ctrl+X and Ctrl+V and right click too.

Many developers are not aware that there are events exist for each of this operation so they achieve it by detecting the keycode on keydown/up event and so on.  This thing made me to share this information here so while reading other post you will come to know about this if not already known to you.

Let’s check the code first.

$(document).ready(function(){

      $('#input1').bind("cut copy paste",function(event) {
           event.preventDefault();
      });

});

That’s it. Above is the only code to restrict cut, copy and paste all together on textbox. Isn’t it cool?

For beginners, let’s discuss how the code is working.

As like jQuery tagline "Write Less, Do More" in above code I have bind multiple events together to the input having id “input1”  and just called a action preventDefult() of event object in it.

event.preventDefault() is an action which prevents the default action to be triggered when called therefore nothing happens when any of the above event gets triggered.

If you want to know more about event.preventDefault() you can visit here.

You can say this solution as cross browser solution as its support almost all browsers if gets used for input element. I have tested it for IE8+, Firefox and Google chrome. For detailed compatibility you can check at this link at quirksmode.org.

Hope you have benefited this simple but useful code.

Feel the joy of sharing by any of the share button provided. Do not forget to share your comments too. Thanks.

Below is live fiddle of the above code.



If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

What is Hoisting in JavaScript?

Hoisting in JavaScript, one of the most misunderstood or little understood concept in the JavaScript and remain famous as interview question for JavaScript developers and now also for angular js developer. Lets check out what it means exactly.


Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.


Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.


How to post JSON string as a string in jQuery AJAX

Know how to post JSON String as string to the action method of controller in MVC.


How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.


Your opinion is valuable for us! Comments, suggetions are welcome.


Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!

x