Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Preventing paste into an input field</h1>
<p>This example demonstrates how to stop a user from pasting content into a HTML input form field using JavaScript. Check the HTML and JavaScript panes for more details or scroll to the bottom to find a code example.</p>

<p>This example was created by <a href="https://dev.to/claireparker/how-to-prevent-pasting-into-input-fields-nn">Claire Parker-Jones</a> and I only forked it in case it was deleted or moved.

<div><label for="paste-ok">✅ You can paste in this field as normal</label></div>
<div><input type="text" id="paste-ok" /></div>

<div><label for="paste-no">🛑 You can't paste in this field: this uses the attribute <code>onpaste</code></label></div>
<div><input type="text" id="paste-no" onpaste="return false" />
</div>

<div><label for="paste-no-event">🛑 You can't paste in this field either: this uses an event listener attached to the input field</label></div>
<div><input type="text" id="paste-no-event" />
</div>

<p>📌 Works in modern browsers. The paste event is part of the Clipboard API. Check out the <a href="https://caniuse.com/#feat=clipboard">caniuse stats</a>.</p>



<h2>Code Example</h2>
<pre><code>&lt;input type="text" id="paste-no-event" /&gt;
&lt;script&gt;
  const pasteBox = document.getElementById("paste-no-event");
  pasteBox.onpaste = e => {
    e.preventDefault();
    return false;
  };
&lt;/script&gt;</code></pre>

<h2>Bypassing in Code</h2>

<p>Any of the following lines of code can remove the protection.</p>

<pre><code>
document.getElementById("paste-no").onpaste={};
document.getElementById("paste-no").onpaste=null;
document.getElementById("paste-no").onpaste=new function(){};
</code></pre>

<p>I can bypass the protection from the console or by DOM editing</p>

<pre><code>
document.getElementById("paste-no-event").value="hello";
</code></pre>
              
            
!

CSS

              
                // All of this is for display only and does not affect the ability to paste.

input {
  height: 35px;
  width: 250px;
  border-radius: 10px;
  border: 1px solid black;
  font-size: 18px;
  padding: 3px;
  margin-bottom: 15px;
}

body {
  font-family: "Open Sans", sans-serif;
  font-size: 18px;
}

pre {
  background-color: #afafaf;
  padding: 20px;
  display: inline-block;
}

              
            
!

JS

              
                const pasteBox = document.getElementById("paste-no-event");
pasteBox.onpaste = e => {
  e.preventDefault();
  return false;
};

// Alternative syntax:

// pasteBox.addEventListener('paste', e => {
//   e.preventDefault();
//   return false;
// });
              
            
!
999px

Console