How to detect copy and paste in javascript?

I have two fields - this is email, and the other is the password in my form. I want to find that when the user re-enters the email address and password, then he should not copy above. he must write again by hand. Like google forms

+4
source share
1 answer

You can disable the combination of ctrl+v and right click .

for IE, you can 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 the user can copy the password fields if the input type is password

Hope this helps.

Note: 1. Disabling JavaScript in the browser will allow the user to do whatever they want 2. Always respect the freedom of users. Keep it in your mind

+7
source

Source: https://habr.com/ru/post/1415281/


All Articles