0
<html>
<head><title>Practice</title>

<script language="javascript" type="text/javascript">  
function disableCopy()  
{  
alert("You cannot perform Copy");  
return false;  
}  

function disablePaste()  
{  
alert("You cannot perform Paste");  
return false;  
}  

function disableCut()  
{  
alert("You cannot perform Cut");  
return false;  
}  


function disableContextMenu()  
{  
alert("You cannot perform right click via mouse as well as keyboard");  
return false;  
}  
</script>
</head>
<body>

Password:<input type="password" oncopy="return disableCopy();" onpaste="return disablePaste();" oncut="return disableCut();" oncontextmenu="return disableContextMenu();"/>

</body>
</html>

How to disable cut , copy and paste operation for password box with alert messages , such that on every operation that will be performed on password box it shows an alert prompt so that user will be notified he can't do cut ,copy and paste operation on passwords.I had gone through other examples which uses onCopy, onCut, onPaste events.I had written functions for this events, but these functions did'nt prompt me the alert message when it was run across all three browsers please provide me help for this situation ,such that it could run across all three browsers and it prompts me an alert when the onCopy, onCut,onPaste event occurs. All I need is when I copy or cut passwords from password field, I need an alert stating that password cannot be copied as the message.

Thanks Friends in advance for the help

Chetan L
  • 41
  • 1
  • 6
  • 3
    Nobody wants to be the user of this. Nobody wants their ordinary, operating-system-wide keyboard shortcuts to raise a modal popup saying they've been blocked. This is user-hostile, and it's potentially for bad security - pasting a long random password from a password safe is a common thing, for example. – TessellatingHeckler Sep 24 '15 at 06:45
  • Why do you want to block the password input? ... There might be other ways to achieve what you want. – Asons Sep 24 '15 at 06:52
  • Disabling pasting will lead you the wrong way... – 3442 Sep 24 '15 at 06:55
  • 1
    possible duplicate of [Disable Copy or Paste action for text box?](http://stackoverflow.com/questions/24424214/disable-copy-or-paste-action-for-text-box) – Rohan Khude Sep 24 '15 at 06:56
  • Could you let me know what is missing in my answer?, so I can adjust and you accept. – Asons May 27 '17 at 11:50

2 Answers2

0

Whether to allow user to perform such actions may depend upon the requirement of the application. To achieve this in vanilla js, you can refer following example.

 document.getElementById('password').addEventListener('copy', function(e) {
   e.preventDefault();
 });
 document.getElementById('password').addEventListener('paste', function(e) {
   e.preventDefault();
 });
 document.getElementById('password').addEventListener('cut', function(e) {
   e.preventDefault();
 });
 document.getElementById('password').addEventListener('drag', function(e) {
   e.preventDefault();
 });
 document.getElementById('password').addEventListener('drop', function(e) {
   e.preventDefault();
 });
Password:
<input type="password" id="password" />
Rayon
  • 36,219
  • 4
  • 49
  • 76
0

One way is using a global handler, where you can handle more than one input by checking its id

window.addEventListener('copy', function(e) {
    if (e.target.id == "password-new") {
       e.preventDefault();
       alert("not allowed");
    }
 });

window.addEventListener('paste', function(e) {
    if (e.target.id == "password-again") {
       e.preventDefault();
       alert("not allowed");
    }
 });

etc...

If you want to catch this in older IE, change this part of the above

    var target = e.target || event.srcElement;
    if (target.id == "password-new") {
       e.preventDefault();
       alert("not allowed");
    }
Asons
  • 84,923
  • 12
  • 110
  • 165