Disabling Copy/Paste for your form using JQuery

There are probably millions of reasons who you would want to disable copy/paste (or indeed the whole right-click menu) on your web page.  My reason was to make it more difficult to copy and paste an email address confirmation.  It’s still possible to copy and paste, but it requires that much effort that you’d probably just be better off inserting the email address again.

The following is updated to include Md Eqbal Ansari’s (see comment below) far more efficient use! A tip of the hat to him!

        $(function () {
            $('input[id$=txtemail]').bind('cut copy paste', function (e) {
                e.preventDefault();
                alert("you cannt " + e.type + " text here!");
            });
        });

Now here’s my now-massively-bloated-by-comparison original! :)

$("#EmailAddressTextBox").get(0).oncontextmenu = function() { return false; };
$("#EmailAddressTextBox").keydown(function(event) {
	//alert(window.event);
	var forbiddenKeys = new Array('c', 'x', 'v');
	var keyCode = (event.keyCode) ? event.keyCode : event.which;
	var isCtrl;
	isCtrl = event.ctrlKey
	if (isCtrl) {
		for (i = 0; i < forbiddenKeys.length; i++) {
			if (forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) {
				//alert('You are prompted to type this twice for a reason!');
				return false;
			}
		}
	}
	return true;

});
$("#ConfirmEmailAddressTextBox").get(0).oncontextmenu = function() { return false; };
$("#ConfirmEmailAddressTextBox").keydown(function(event) {
	//alert(window.event);
	var forbiddenKeys = new Array('c', 'x', 'v');
	var keyCode = (event.keyCode) ? event.keyCode : event.which;
	var isCtrl;
	isCtrl = event.ctrlKey
	if (isCtrl) {
		for (i = 0; i < forbiddenKeys.length; i++) {
			if (forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) {
				//alert('You are prompted to type this twice for a reason!');
				return false;
			}
		}
	}
	return true;
});

Thanks to these sources:
http://stackoverflow.com/questions/187229/why-….-textbox
http://forums.asp.net/t/1125227.aspx

6 thoughts on “Disabling Copy/Paste for your form using JQuery

  1. Hi, thanks for your post
    I’m making a “typeracer-wannabe” app with jQuery, and stumble on your blog.

    Instead of using a loop (for), I just use a trick from jQuery Cookbook,
    var forbiddenKeys = ‘,x,c,v,';
    if (forbiddenKeys.indexOf(‘,’ + String.fromCharCode(keyCode).toLowerCase() + ‘,’) > -1) {
    return false;
    }

  2. Hi sangprabo,

    You solution is great, but when i implemented it, numeric keypad number 3 is not working, i amnot sure which forbiddenkey is causing the issue. But Christopher Hubbard logic is working as expected.

    Regards
    John.

  3. hello All,

    the following code restrict user to cut copy paste in textbox
    ————————————————–

    $(function () {
    $(‘input[id$=txtemail]‘).bind(‘cut copy paste’, function (e) {
    e.preventDefault();
    alert(‘you cannt ‘ + e.type + ” text here!”);
    });
    });

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>