How to Prevent Pasting into an Input
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.
![5 Awesome New Mozilla Technologies You’ve Never Heard Of]()
My trip to Mozilla Summit 2013 was incredible. I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out. MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...
![Detect DOM Node Insertions with JavaScript and CSS Animations]()
I work with an awesome cast of developers at Mozilla, and one of them in Daniel Buchner. Daniel's shared with me an awesome strategy for detecting when nodes have been injected into a parent node without using the deprecated DOM Events API.
![Chris Coyier’s Favorite CodePen Demos II]()
Hey everyone! Before we get started, I just want to say it's damn hard to pick this few favorites on CodePen. Not because, as a co-founder of CodePen, I feel like a dad picking which kid he likes best (RUDE). But because there is just so...
![Using Opacity to Show Focus with MooTools]()
I'm a huge fan of using subtle effects like link nudging (jQuery, MooTools) to enhance the user experience and increase the perceived dynamism of my websites. Trust me -- a lot of little things are what take websites to the next level.
So am I able to edit the locally stored js file to re-enable pasting until I reload the page?
Thanks a lot for sharing this. It was really needed as I don’t like to write passwords and username again and again.