How to block copying and pasting in an input?

2

I have an input ...

<input type="text">

How do I block the commands (control + c and control + v) and right click, copy or paste?

    
asked by Necroyeti 05.12.2017 в 20:50
source

2 answers

6

With JavaScript native you could do the following:

window.onload = function() {
  var myInput = document.getElementById('bloquear');
  myInput.onpaste = function(e) {
    e.preventDefault();
    alert("esta acción está prohibida");
  }
  
  myInput.oncopy = function(e) {
    e.preventDefault();
    alert("esta acción está prohibida");
  }
}
<input type="text" id="bloquear">

Or with jQuery :

$(document).ready(function(){
  $("#bloquear").on('paste', function(e){
    e.preventDefault();
    alert('Esta acción está prohibida');
  })
  
  $("#bloquear").on('copy', function(e){
    e.preventDefault();
    alert('Esta acción está prohibida');
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="bloquear">
    
answered by 05.12.2017 / 20:57
source
6

Using html, you can add to your <body>

oncopy="return false" to avoid copying ( Ctrl + c ),

and onpaste="return false" to avoid hitting ( Ctrl + v ):

Example:

<body oncopy="return false" onpaste="return false">    
<input type="text">

This is a demo:

    <body oncopy="return false" onpaste="return false">    
    <input type="text">
    <p>Trata de seleccionar y copiar este texto (Ctrl+c) y pegarlo (Ctrl+v) ...</p>
    
answered by 05.12.2017 в 20:57