Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do Sites Suppress Pasting Text?

I've noticed that some sites (usually banks) suppress the ability to paste text into text fields. How is this done? I know that JavaScript can be used to swallow the keyboard shortcut for paste, but what about the right-click menu item?

like image 204
John Topley Avatar asked Aug 28 '08 18:08

John Topley


People also ask

How do websites block copy and paste?

The most common way websites use to disable copying and right click involves use of JavaScript, whereas others use simple styling properties to disable text selection.

Why do websites disable paste?

Most bank and financial websites don't allow users to copy a piece of text to the clipboard using the mouse or keyboard shortcut for security reasons. For example, I recently used a popular stock broking website called Groww, which doesn't allow me to copy anything to the clipboard, and they are not the only ones.

How do I copy text from a copy restricted website?

To copy text from a blocked site in Chrome, disable JavaScript on the site. When Chrome tries to load the page, any JavaScript commands are disabled as well, allowing you to copy the text.


2 Answers

Probably using the onpaste event, and either return false from it or use e.preventDefault() on the Event object.

Note that onpaste is non standard, don't rely on it for production sites, because it will not be there forever.

$(document).on("paste",function(e){
  console.log("paste")
  e.preventDefault()
  return false;
})
like image 177
bdukes Avatar answered Nov 03 '22 23:11

bdukes


Even if it is somewhat possible to intercept the paste event in many browsers (but not all as shown at the link on the previous answer), that is quite unreliable and posible not complete (depending on the browser / OS it may be possible to do the paste operation in different ways that may not be trappable by javascript code).

Here is a collection of notes regarding paste (and copy) in the context of rich text editors that may be applied also elsewhere.

like image 32
LucaM Avatar answered Nov 04 '22 00:11

LucaM