Disable Copy Paste Right Click Using JavaScript In Asp.Net TextBox

In this example i'm explaining how to Disable Copy Paste Right Click Using JavaScript In Asp.Net TextBox, Some time for many reasons we don't want to allow users to use right click to copy paste or by using ctrl+C , ctrl+v in textbox on a aspx page in asp.net, To disable this we can use javascript

we can achieve this in 2 ways

1. use this method when u don't want any alerts or message

<asp:TextBox ID="TextBox1" runat="server"
oncopy="return false"
onpaste="return false"
oncut="return false">
</asp:TextBox>


2. If you want to show alerts than use this method instead
Right this javascript function in the head section of aspx page, in this function we are disabling right mouse click and ctrl keys
<head runat="server">
<title>Untitled Page</title>
<script language="javascript">
function DisableRightClick(event)
{
//For mouse right click 
if (event.button==2)
{
alert("Right Clicking not allowed!");
}
}
function DisableCtrlKey(e)
{
var code = (document.all) ? event.keyCode:e.which;
var message = "Ctrl key functionality is disabled!";
// look for CTRL key press
if (parseInt(code)==17)
{
alert(message);
window.event.returnValue = false;
}
}
</script>
</head>

Now use this function on the textbox which we want to disable copy paste and right clicking
<body>
<form id="form1" runat="server">
<div>
<strong>
Right click disabled</strong> textbox
<br />
<asp:TextBox ID="TextBoxCopy" runat="server"
onMouseDown="DisableRightClick(event)">
</asp:TextBox><br />
<br />
<strong>Ctrl key </strong>disabled<br />
<asp:TextBox ID="TextBox2" runat="server"
onKeyDown="return DisableCtrlKey(event)">
</asp:TextBox><br />
<br />

Another method to disable<strong> Cut,Copy and paste
</strong>in textbox<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"
oncopy="return false"
onpaste="return false"
oncut="return false">
</asp:TextBox>
</form>
</body>
Hope this helps


If you like this post than join us or share

35 comments:

Anonymous said...

very good article


Anonymous said...

Gr8 man.
Thanks a lot


Anonymous said...

thanxs a lot


Anonymous said...

Good Article amiT jaiN ..


Anonymous said...

Very direct and usefull


Anonymous said...

Thanks for the code. it really worked.
sapien4u@gmail.com


Anonymous said...

This is good, why are these values not showing up in Intellisense, they work but are they unsupported?


Anonymous said...

thanks it works!


bayu said...

thanks Bro.........


2E2 2011 Rocks! said...

Help! I Dont Know Where To Paste!


Anonymous said...

Jain sahab Kamal kar diya......


Anonymous said...

Great!!! Thanks a lot.
Code are easy to read and understand compare with other search I have found.


John said...

Thanks a lot . Very helpful for starters.


Anonymous said...

very good post, thanks.


Anonymous said...

Thanks alot.
Easy to understand Good work.


Anonymous said...

thanks mate


Unknown said...

Good work...Keep Going.....:)


prabha said...

thank u very much...]

it was very use ful to me...


Anonymous said...

great man 10q


Anonymous said...

Good post.Thanks


Anonymous said...

Gracias, era lo que buscaba.


Anonymous said...

Awesome........


Anonymous said...

very good post.but i also want to make these text box data enable to copy on button click..help me asap.


Anonymous said...

its good,,but failed if user use select all command,then anyone can copy all data.any one makes changes in this code very easily....
pls try to give best solution for this


Anonymous said...

please suggest how i can apply the same for complete html page inspite of for a particular control.


Kalyan said...

nice...got what i was looking for...Thank you...amiT jaiN....


Anonymous said...

thanx buddy..it was really helpful


muni said...

YA GOOD ONE....


Anonymous said...

good article


Anonymous said...

Very impressive...


Some thing by Mahmood Memon said...

Amazing article but, I have a confusion that when I remove alert from it. It do nothing. I checked it with return false, window.event.returnValue=false. But nothing happens. Please help me


Some thing by Mahmood Memon said...

I got new one
Just write in body tag
oncontextmenu="return false;"


Vipul said...

Awesome article Amit!! Gr8 job..


Unknown said...

Thanks a lot vipul, keep visiting


Vishal Barot said...

This comment has been removed by a blog administrator.


Find More Articles