How to Prevent Pasting into an Input
Publikováno: 19.10.2020
Every once in a while I get to a website that doesn’t allow me to paste into a form input. In most cases it’s something to do with login credentials (username and or password) and auth codes. So how are they preventing me from pasting information? It’s as easy as you’d think! Let’s start with […]
The post How to Prevent Pasting into an Input appeared first on David Walsh Blog.
Every once in a while I get to a website that doesn’t allow me to paste into a form input. In most cases it’s something to do with login credentials (username and or password) and auth codes. So how are they preventing me from pasting information? It’s as easy as you’d think!
Let’s start with the input
element:
<input type="text" onpaste="return false;" ondrop="return false;" autocomplete="off" />
The onpaste
attribute lets us prevent pasting into the form. Adding the autocomplete
attribute as well as preventing drag and drop into the element. If you want to avoid the on{event}
code in the HTML, you can do it the cleaner way:
myElement.addEventListener('paste', e => e.preventDefault());
Writing this post pains me because I loathe when websites prevent me from pasting text. I’m begging you not to do this. Just don’t.
The post How to Prevent Pasting into an Input appeared first on David Walsh Blog.