Preventing users from copying text and pasting it into a textbox

2
Is there a way to do not allow a user to paste text Into a textbox?
asked
4 answers
3

Hi Masterman,

Add HTMLsnippet to your form and add this to its JavaScript option:

document.onpaste=new Function("return false");

OR use following jQuery instead (in JavaScript option again) :

$(document).ready(function(){
  setTimeout(function(){ $(":text").bind("cut copy paste",function(e) { e.preventDefault();  });  },500)
})

the second solution is cross browser compatible as long as your application support jQuery (Tested in Chrome , FF and IE)

Please note first solution will disable paste on everything on your form and second will disable cut/copy/paste on all input text element. (feel free to remove cut and copy from code if you wish to disable paste only) .If you prefer to target only one text box you could assign its Id instead something like

 function(){ $(“#id”)....

Good Luck Ben

answered
1

I found a few threads on StackOverflow about this, so I recommend Googling it a bit. Generally the feeling is that, from a UX perspective, this is not recommended. So, perhaps there's another route you could persue.

However, I fiddled with this concept for a bit, and while I didn't finish, it may get you most of the way there. If you set your input widget's class to "disablepaste", and find a way to execute this javascript after the page has loaded, it will work on IE9+ and most other browsers:

var inputList = document.getElementsByClassName("disablepaste");
var preventer =  function(e) {e.preventDefault();};
for (var i=0; i < inputList.length; i++) {
    var inputs = inputList[i].getElementsByTagName('input');
    for (var j=0; j < inputs.length; j++) {
        inputs[j].addEventListener("paste", preventer, true);
    }
}

I was only able to run this code using Chrome's Dev Tools (javascript console), but when I run it there, it does block pasting to my input field.

So, find a way to run this after your input field has loaded and you have a solution. Good luck!

answered
0

I assume that setting the property Editable of that text-box to 'Never' is not the solution for you?

answered
0

Thanks Frank for your reply, but I need the user to be able to enter text into the textbox,but at the same time I don't want them to paste text into it

answered