admin管理员组

文章数量:1356778

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.

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.

Share Improve this question asked Mar 17, 2015 at 19:23 RamsonRamson 2294 silver badges12 bronze badges 4
  • duplicate of this: stackoverflow./a/12806002/1726419 – yossico Commented Mar 17, 2015 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 Commented Mar 17, 2015 at 19:36
  • possible duplicate of Disable copy paste in HTML input fields? – arthurakay Commented Mar 17, 2015 at 19:48
  • @Ramson - The example in the link IS html&JS solution - have you tried it? – yossico Commented Mar 17, 2015 at 22:25
Add a ment  | 

2 Answers 2

Reset to default 8

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();
});

The only reasonable way would be to use JavaScript to catch the CTRL/CMD+V key bo 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:

  1. Any solution will rely on JavaScript, which can be disabled.
  2. The browser may have a application-level menu the user can use to paste (Edit > Paste).
  3. 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.

本文标签: javascriptHow to restrict pasting text in an input text field in htmlStack Overflow