Javascript 禁用 HTML 输入字段中的复制粘贴?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12805803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:30:31  来源:igfitidea点击:

Disable copy paste in HTML input fields?

javascripthtmlsecuritypasswordscopy-paste

提问by Adam Matan

Possible Duplicate:
Disable Copy/Paste into HTML form using Javascript

可能的重复:
使用 Javascript 禁用复制/粘贴到 HTML 表单

My bankseem to have disabled copy-paste of username and password.

我的银行似乎禁用了用户名和密码的复制粘贴。

How is it done? Does it improve security?

它是如何完成的?它提高了安全性吗?

回答by Ilya Sidorovich

You can disable paste in your input as follows:

您可以按如下方式在输入中禁用粘贴:

html:

html:

<input type="text" value="" id="myInput">

javascript:

javascript:

window.onload = function() {
 const myInput = document.getElementById('myInput');
 myInput.onpaste = function(e) {
   e.preventDefault();
 }
}

Talking about security, I wouldn't say that this makes any impact. You would usually use client side and well as server-side validation of data submitted by the user.

谈到安全性,我不会说这会产生任何影响。您通常会使用客户端和服务器端验证用户提交的数据。

Hope this helps

希望这可以帮助