30

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

1

3 Answers 3

39

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;" />
0
9

Javascript supports onpaste:

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

1
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);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.