29

I have a input box, and I would like to use vbscript or javascript (no jquery) to capture the paste event.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
Itay.B
  • 3,991
  • 14
  • 61
  • 96
  • Check answers https://stackoverflow.com/questions/3211505/detect-pasted-text-with-ctrlv-or-right-click-paste/68277966#68277966 – MD SHAYON Jul 06 '21 at 22:21

3 Answers3

38

Use the onpaste event to capture the event and do what you need in Javascript. E.g. to disable the paste in an input text field:

<input type="text" onpaste="return false;" />
philant
  • 34,748
  • 11
  • 69
  • 112
9

Javascript supports onpaste:

http://www.quirksmode.org/dom/events/cutcopypaste.html

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • 1
    Mozilla says this isn't on any standards track: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/onpaste – Michael Cole Dec 24 '15 at 02:12
6

Just for future readers finding this as I did.

You will still be able to drop text into an input with the onpaste="return false;" attribute. If you want to avoid this, you can do something like this:

var input_element = document.getElementById("Element");
input_element.addEventListener("drop", function (event) {
    var types = event.dataTransfer.types;
    
    if (types.length > 2 || types.indexOf("text/plain") === -1)
        event.preventDefault();
    else {
      setTimeout(function () { input_element.value = ""; }, 10);
    }
}, false);
Sebastian Td
  • 174
  • 1
  • 7