如何检测复制和粘贴在JavaScript? [英] how to detect copy and paste in javascript?

查看:81
本文介绍了如何检测复制和粘贴在JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字段,一个是emailid,另一个是我的表单中的密码。我想要检测到,当用户重新输入电子邮件和密码比他不应该能够复制上述。他必须手动再次写入。就像google表格一样 v 组合和右键点击

你可以使用;

  onpaste =return false; oncut =返回false; oncontextmenu =返回false; oncopy =返回false。 

这是所有浏览器的解决方法;

 函数noCTRL(e)
{
var code =(document.all)? event.keyCode:e.which;
var ctrl =(document.all)? event.ctrlKey:e.modifiers& Event.CONTROL_MASK;
var msg =对不起,此功能已禁用。;
if(document.all)
{
if(ctrl& code == 86)// CTRL + V
{
alert(msg);
window.event.returnValue = false;

else if(ctrl& code == 67)// CTRL + C(复制)
{
alert(msg);
window.event.returnValue = false;

$ b $ else {
if(ctrl == 2)// CTRL键
{
alert(msg);
返回false;
}
}
}
电子邮件地址:< input name =emailtype =textvalue =/>< br />
密码:< input name =passwordtype =passwordvalue =/>< br />
确认电子邮件地址:< input name =emailtype =textvalue =onkeydown =return noCTRL(event)/>
确认密码:< input name =passwordtype =passwordvalue =onkeydown =return noCTRL(event)/>

如果输入类型为,我不认为用户可以复制密码字段密码



希望这有助于您。

注意:
1.在浏览器中禁用JavaScript将允许用户做任何他想要的
2.始终尊重用户的自由。请记住这一点


i have two field one is emailid and another is password in my form. i want to detect that when user reenter the email and password than he should not able to copy the above. he have to write again manually.Just like google forms

解决方案

You could disable ctrl+v combination and right click as well.

for IE, you may use;

onpaste="return false;" oncut="return false;" oncontextmenu="return false;" oncopy="return false;".

Here is a workaround for all browsers;

function noCTRL(e)
{
var code = (document.all) ? event.keyCode:e.which;
var ctrl = (document.all) ? event.ctrlKey:e.modifiers & Event.CONTROL_MASK;
var msg = "Sorry, this functionality is disabled.";
if (document.all)
{
if (ctrl && code==86) //CTRL+V
{
alert(msg);
window.event.returnValue = false;
}
else if (ctrl && code==67) //CTRL+C (Copy)
{
alert(msg);
window.event.returnValue = false;
}
}
else {
if (ctrl==2) //CTRL key
{
alert(msg);
return false;
}
}
}
Email :<input name="email" type="text" value=""/><br/>
Password :<input name="password" type="password" value=""/><br/>
Confirm Email :<input name="email" type="text" value="" onkeydown="return noCTRL(event)"/>    
Confirm Password :<input name="password" type="password" value="" onkeydown="return noCTRL(event)"/>

and I don't think user can copy password fields if input type is password

Hope this helps.

Note: 1. Disabling JavaScript in browser will let user do whatever he want 2. Always respect user's freedom. Keep this in your mind

这篇关于如何检测复制和粘贴在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆