79

How can I disable Paste (Ctrl+V) option using jQuery in one of my input text fields?

Abhishek
  • 6,912
  • 14
  • 59
  • 85
Misam
  • 4,320
  • 2
  • 25
  • 43
  • 12
    Why would you do that? It would frustrate more then one user if you disable ctrl+v. besided you can always right click and select paste – Christophe Apr 01 '11 at 07:06
  • 8
    @krike maybe to force people to retype their email address or password? – greggreg Apr 01 '11 at 07:11
  • 5
    I will disable JS in browser and can put ctrl+v anytime. – Kamilos Apr 01 '11 at 07:31
  • 2
    @Kamilos what if the site requires javascript to run? – greggreg Apr 01 '11 at 07:32
  • 7
    @Kamilos what about banking or facebook? if you disable javascript we cant even post comments in stack overflow. – greggreg Apr 01 '11 at 07:57
  • 18
    @greg: "to force people to retype their email address" - and pray tell, what is the point in that? If you are already setting up pointless tricks like "type in your address twice, to see if you actually remember it", and then mess with my clipboard, just to show me who's in charge, I'm leaving - if *that* is an attempt to make a good first impression, then I fully expect the site to be terrible. – Piskvor left the building Apr 01 '11 at 11:46
  • 3
    Based on a true story: I know this is old, but here's a perfectly valid case in where to ask the user to retype the email address: The user is buying some online tickets for an event, he is not registered in the website and the only way he'll receive his tickets is in the provided email address. If the user misspells the address, he won't receive the tickets. Then he'll blame the "crappy system that doesn't work and don't send the tickets" and give the support team more work by tons of emails and phone calls – hectorg87 Apr 21 '14 at 08:10
  • 1
    Disabling paste is a security nightmare. Sensible users use a password manager with long random passwords that no human can remember. The user copies and pastes the passwords from the password manager to the login, thus benefitting from strong, unique-per-web site passwords and resilience to keyboard logging malware and hardware. – David Johnston Oct 13 '15 at 05:02
  • @DavidJohnston password managers now have an auto-fill option – David Gras Nov 23 '15 at 10:37
  • 1
    @daVe ...which have their own security implications. Also a "smart" web page might prevent that too. – David Balažic Apr 06 '17 at 12:41
  • Another situation where this would be valid is the text input field, which simulates number/decimal input (it may be the best solution if you want to set proper decimal separator for multilanguage apps, as default number/decimal input does no handle it very well). Obviously you may want to prevent users from pasting some invalid strings there. So I would rather focus on the question itself than dispute whether someones solution is proper or not, especially if we don't know any details. – Daniel Stasiak Apr 10 '20 at 07:43

11 Answers11

194

This now works for IE FF Chrome properly... I have not tested for other browsers though

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

Edit: As pointed out by webeno, .bind() is deprecated hence it is recommended to use .on() instead.

Misam
  • 4,320
  • 2
  • 25
  • 43
  • 12
    $('#txtInput').bind("contextmenu",function(e){ e.preventDefault(); });//This disables the right-click – Misam Apr 01 '11 at 09:21
  • 3
    Interesting, I didn't know these events existed. I've found this page on browser quirks mode that gives info on cross browser compatability for these events: http://www.quirksmode.org/dom/events/cutcopypaste.html – DannyLane Apr 01 '11 at 12:51
  • Safari supports this too. – Hannes Karppila Jun 09 '15 at 15:40
  • wow, this is great! You can disable the contextmenu but the greatest thing is that it's not necesary! – David Gras Nov 23 '15 at 10:43
  • 1
    **NOTE** "As of jQuery 3.0, .bind() has been deprecated. It was superseded by the .on() method for attaching event handlers to a document since jQuery 1.7, so its use was already discouraged." -- Source: http://api.jquery.com/bind/ – benomatis Mar 09 '18 at 20:35
  • ***`cut`, `copy`, and `paste` are experimental (but i believe widely supported)*** – oldboy Nov 07 '20 at 00:09
21

Edit: It's almost 6 years later, looking at this now I wouldn't recommend this solution. The accepted answer is definitely much better. Go with that!


This seems to work.

You can listen to keyboard events with jQuery and prevent the event from completing if its the key combo you are looking for. Note, check 118 and 86 (V and v)

Working example here: http://jsfiddle.net/dannylane/9pRsx/4/

$(document).ready(function(){
    $(document).keydown(function(event) {
        if (event.ctrlKey==true && (event.which == '118' || event.which == '86')) {
            alert('thou. shalt. not. PASTE!');
            event.preventDefault();
         }
    });
});

Update: keypress doesn't fire in IE, use keydown instead.

DannyLane
  • 2,096
  • 1
  • 16
  • 17
13

As of JQuery 1.7 you might want to use the on method instead

$(function(){
    $(document).on("cut copy paste","#txtInput",function(e) {
        e.preventDefault();
    });
});
dma_k
  • 10,431
  • 16
  • 76
  • 128
Steve C
  • 2,638
  • 3
  • 25
  • 27
4

I tried this in my Angular project and it worked fine without jQuery.

<input type='text' ng-paste='preventPaste($event)'>

And in script part:

$scope.preventPaste = function(e){
   e.preventDefault();
   return false;
};

In non angular project, use 'onPaste' instead of 'ng-paste' and 'event' instesd of '$event'.

Abhishek
  • 346
  • 6
  • 17
4

The following code will disable cut, copy and paste from full page.

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

The full tutorial and working demo can be found from here - Disable cut, copy and paste using jQuery

JoyGuru
  • 1,803
  • 20
  • 11
4

 $(document).ready(function(){
   $('#txtInput').on("cut copy paste",function(e) {
      e.preventDefault();
   });
});
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="text" id="txtInput" />
Gtm
  • 465
  • 4
  • 12
4
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;
});
hawx
  • 1,629
  • 5
  • 21
  • 37
0
$(document).ready(function(){
  $('#txtInput').live("cut copy paste",function(e) {
    e.preventDefault();
  });
});

On textbox live event cut, copy, paste event is prevented and it works well.

Vikas Bansal
  • 148
  • 2
  • 7
0

I have tested the issue on chrome browser and it is working for me.Below is a solution for preventing the paste code in your textbox and also prevent the right click.

   $(".element-container").find('input[type="text"]').live("contextmenu paste", function (e) {

    e.preventDefault();
});
Chirag Prajapati
  • 529
  • 6
  • 10
0

You can catch key event :

function checkEventObj ( _event_ ){
    // --- IE explorer
    if ( window.event )
        return window.event;
    // --- Netscape and other explorers
    else
        return _event_;
}

document.keydown = function(_event) {
    var e = checkEventObject(_event);

    if( e.ctrlKey && (e.keyCode == 86) )
        window.clipboardData.clearData();
}

Not tested but, could help.

Source from comentcamarche and Zakaria

canardman
  • 3,103
  • 6
  • 26
  • 28
  • 2
    `window.clipboardData.clearData()` only works in ie. If you're catching the event you might as well just prevent default and return false. that would effectively disable keyboard initiated paste. I'd post the example but i'm too lazy, and plus you're already so close. – greggreg Apr 01 '11 at 07:28
-2

$(document).ready(function(){
   $('input').on("cut copy paste",function(e) {
      e.preventDefault();
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
waheed
  • 73
  • 1
  • 1
  • 5