I need to lock Ctrl+v in a text box

Asked

Viewed 14,813 times

9

I want to create an input type="text" which does not allow them to glue anything to it. You do not need to present any alert or anything like that, just don’t allow the necklace.

  • 2

    Removing native features, in addition to not being 100% efficient and safe, can still confuse and frustrate the user. Such an approach should be avoided to the maximum.

  • 1

    this can help you: http://stackoverflow.com/questions/15394957/how-to-disable-ctrl-c-v-using-javascript-for-both-internet-explorer-and-firefox

  • I actually need for an online proof that this approach was needed.

4 answers

16


If you are using jQuery in your application, you can do it as follows:

In your HTML

<input type="text" id="texto"></input>

In your Javascript

$(document).ready(function() {

    $("#texto").bind('paste', function(e) {
        e.preventDefault();
    });

});

If you need examples of how to detect copy and cut text, in this post there are some cool examples:

http://www.mkyong.com/jquery/how-to-detect-copy-paste-and-cut-behavior-with-jquery/

8

Just to add an alternative to the above mentioned options:

<input type="text" onpaste="return false" ondrop="return false"/>

The attribute onpaste takes the necklace event and the ondrop dragging. In this way, the user will be prevented either from pasting text (Ctrl+v or right mouse button) or from dragging text with the mouse into this field.

3

To avoid Crtl+C Crtl+V or Crtl+X, you can use it as well...

 $(document).ready(function() {
     $('#texto').bind('cut copy paste', function(event) {
         event.preventDefault();
     }); 
 });
  • the author of the question only needs to know how to avoid colar, and your method is the same as the other answer

3

If it’s pure javascript, this template inline serves:

document.getElementById('texto').onpaste = function(){
    return false;
}

If you do not want to use inline mode, you can do so (IE > 9):

document.getElementById('texto').addEventListener(
    'paste', 
    function(E){
        E.preventDefault();
        return false;
    },
    true
);

In this mode, if you need to include IE below version 10, you will need to condition the expressions using attachEvent for IE and addeventlistener for the rest of the browsers.

Good luck.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.