I use html5, jQuery, css for web designing. Can anyone please tell me how to restrict pasting data in an input text field in html. I searched for the same but I did find restricting copying but not pasting. Please help me. Thanks.
-
duplicate of this: http://stackoverflow.com/a/12806002/1726419 – yossico Mar 17 '15 at 19:26
-
@yossico Hi I am looking for a jquery or html related solution and thats why I posted it as my own question. Thanks. – Ramson Mar 17 '15 at 19:36
-
possible duplicate of [Disable copy paste in HTML input fields?](http://stackoverflow.com/questions/12805803/disable-copy-paste-in-html-input-fields) – arthurakay Mar 17 '15 at 19:48
-
@Ramson - The example in the link IS html&JS solution - have you tried it? – yossico Mar 17 '15 at 22:25
2 Answers
Using jquery, you could avoid paste using this
$('.textboxClass').on('paste', function(e) {
e.preventDefault();
});
you can avoid copy paste and cut using this
$('.textboxClass').on('copy paste cut', function(e) {
e.preventDefault();
});

- 14,926
- 5
- 42
- 56
The only reasonable way would be to use JavaScript to catch the CTRL/CMD+V key combo and prevent it from doing anything and also using JavaScript to disable right-click on the input to prevent the user from using the context menu to paste.
However, there's caveats:
- Any solution will rely on JavaScript, which can be disabled.
- The browser may have a application-level menu the user can use to paste (Edit > Paste).
- Even if JavaScript is enabled, some browsers allow the user to specifically disable the ability of a website to disable right-clicking.
In other words, there's no fool-proof method to prevent this. If the user is motivated enough, they can easily bypass any restricting you place on this input. Even if you were to disable the field entirely to prevent any modification, paste or otherwise, an industrious user can utilize their browsers dev tools to either remove the disabled attribute on the input or update the value attribute directly.
In general, trying to restrict what a user can do is always a bad move. The goal of good UI should be to enable the user, not to apply arbitrary and often frustrating restrictions.

- 232,153
- 36
- 385
- 444