2

I want to prevent people from pasting password in login form. Is it possible by PHP to disable the ability to paste into input fields.

MI Sabic
  • 367
  • 2
  • 7
  • 18
  • 8
    You can't do this by `PHP` because its a server side language. You have to use `Javascript/Jquery` for this. – Manwal Apr 17 '15 at 06:02
  • 4
    What in all of Earth are you trying to do with this? Stop people from using password managers and consequently encourage them to use a weak password just so they can remember it or type it easily? – lc. Apr 17 '15 at 06:02
  • possible duplicate of [Disable Copy/Paste into HTML form using Javascript](http://stackoverflow.com/questions/1226574/disable-copy-paste-into-html-form-using-javascript) – Manwal Apr 17 '15 at 06:04
  • ok i got it. thanks to everybody – MI Sabic Apr 17 '15 at 06:19
  • does any answer helped you? if so please mark the correct answer which helped you to solve the problem. this way we can improve the stackoverflow community. please – Amit Shah Apr 24 '15 at 06:24

6 Answers6

6

You can simply do this with HTML by adding some properties to input.

I don't know why you want to use PHP for this.

<input type="test" onCopy="return false" onDrag="return false" onDrop="return false" onPaste="return false"/>
Manwal
  • 23,450
  • 12
  • 63
  • 93
2

Use below code HTML

<input type="password" id="pwd">

Jquery 

 $('#pwd').bind("cut copy paste",function(e) {
          e.preventDefault();
      });
Mahadeva Prasad
  • 709
  • 8
  • 19
2

In Jquery you can do like this to disable copy paste on whole Page

$('body').bind('copy paste',function(e) {
    e.preventDefault(); return false; 
});
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
0

Use this to disable paste in your input as follows:

html:

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

javascript:

window.onload = function() {
 var myInput = document.getElementById('myInput');
 myInput.onpaste = function(e) {
   e.preventDefault();
 }
}
Rex Rex
  • 1,030
  • 1
  • 8
  • 29
0

You can not do it using php, because it is server side scripting language. you will have to handle this situation using javascript or Jquery

Following are the options which can help.

$(document).ready(function(){
      $('#txtInput').bind("cut copy paste",function(e) {
          e.preventDefault();
      });
    });



jQuery('input.disablePaste').keydown(function(event) {
    var forbiddenKeys = new Array('c', 'x', 'v');
    var keyCode = (event.keyCode) ? event.keyCode : event.which;
    var isCtrl;
    isCtrl = event.ctrlKey
    if (isCtrl) {
        for (i = 0; i < forbiddenKeys.length; i++) {
            if (forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) {
                 return false;
            }
        }
    }
    return true;
});

Thanks Amit

Amit Shah
  • 1,380
  • 1
  • 10
  • 19
0

$(document).ready(function() {
  $('#password').bind("cut copy paste", function(event) {
    event.preventDefault();
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="password" id="password">
Dhaval Bharadva
  • 3,053
  • 2
  • 24
  • 35