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
2 Answers
Reset to default 8Using 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:
- 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.
本文标签: javascriptHow to restrict pasting text in an input text field in htmlStack Overflow
版权声明:本文标题:javascript - How to restrict pasting text in an input text field in html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743979316a2571011.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论