How to disable copy-paste functionality (cntrl C/V) in Asp.net textbox

For many security purposes, sometimes we need not to allow users to copy/paste centain information into some asp.net controls like TextBox. This may include email address, password etc.

As we know copy/paste can be done using either the right click of the mouse or using keys control+c, control+v etc.

So to disallow users to copy or paste we need to disable right click of the mouse as well as control+c, control+v on the required controls (textbox etc).

1. Disable right click:

We should use the javascript functions to achieve this.

<script language=”javascripttype=”text/javascript“>

 var message=”Sorry, Right Click is disabled.“;

//this will register click funtion for all the mousedown operations on the page

document.onmousedown=click;

function click(e) {

//for Internet Explore..’2′ is for right click of mouse

if (event.button == 2) {

             alert(message);

            return false;

}

//for other browsers like Netscape 4 etc..

if (e.which == 3){

            alert(message);

            return false;

}

}

</script>

:This will disable right click on the whole page, if we want to disable it only for some textbox we should check the source of the event using e.srcElement.

2. Disable cntrl+c, cntrl+v:

Lets consider a textbox with id=txtsubject. First register the event handlers for copy & paste events on this textbox.

This can be done in Page_Load() event handler as:

txtSubject.Attributes.Add(“onpaste“, “JavaScript: DisableCopyPaste();”);   //for paste

txtSubject.Attributes.Add(“oncopy“, “JavaScript: DisableCopyPaste();”);   //for copy

Now DisablePaste() funtion in Javascript:

function DisableCopyPaste()

        {

            alert(‘This functionality has been disabled !‘);            

            window.clipboardData.clearData(“Text“); //for cleaning up the clipboard

            // Cancel default behavior

            event.returnValue = false;

        }

Advertisements
This entry was posted in .Net 2.0. Bookmark the permalink.

15 Responses to How to disable copy-paste functionality (cntrl C/V) in Asp.net textbox

  1. Robor says:

    Hi there,
    Thank you! I would now go on this blog every day!

    Thank you
    Robor

  2. Bhim says:

    Hi Munish,

    I’ve just started a blog through WordPress.com. What I’m doing now is: just to cut/copy and paste the news n info to the blog. I don’t have any computer software and webpage designing knowledge.

    So, could you please teach me A,B,C,D…..about how to disable copy-paste functionality? Actually, I’m confused about- from where to start? Where to put these Javascript (that you’ve mentioned above) while publishing an article…., etc?

    I’ll really appreciate your help/guidance.

  3. Ganesh Mohan says:

    Hi Munish,

    The right click, Ctrl A, Ctrl C, Ctrl V works for IE and Netscape(Mozilla). Even I succeded by blocking PrintScreen in IE by using clipboardData.clearData();
    BUT… I am not able to make it work in Netscape(Mozilla). So you have any Idea about blocking PrintScreen button or clearing clip board data in Mozilla.

    Thanks in Advance
    Ganesh Mohan

  4. Hi Ganesh,

    I never tried in Mozilla, but the below link may help you out in this.

    http://www.infogears.com/cgi-bin/infogears/mozilla_firefox_copy_paste.html

  5. Salman says:

    It is working for IE but not for FireFox. Any suggestions.

  6. Shiva says:

    i am new to asp and c# now only started learning, could you please send the coding how to disable the cut copy paste events for a text box in c#.

  7. Kannan says:

    Nice post. Timely helped me…

  8. Faiz Shaikh says:

    For Firefox please try this

  9. G.Devendar Rao says:

    how to disable print screen and clipboard activities in sharepoint 2007.. help me guys

  10. Ahmad says:

    Wah Ra ceto Blasssss

  11. palani says:

    restriction is working, but through email is not working.can u suggest me how to restrict though email.am going to give only view permission to some user and save permission to some user.

    reply asap.

  12. nathan1221 says:

    Hi Munish,
    I am quite new in website blogging, Could you please inform how to put above java script on my blog, I try go to dasboard on my site admin, but I cant see in which feature I can put that’s above script, Thank you in advanced

    Nathan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s