Many a times you would have observed that websites does not allow you to copy and paste the password or some other stuff in the text box.
In this article we will see how that functionality can be achieved in few lines of code using jquery.
Step 1. Create an aspx page with following markup.
Step 2. In the $(document).ready(function(){}) of jquery inject the following scritp(self explanatory).
Note : you can get more info on .bind from www.jquery.com and DOM event list from http://en.wikipedia.org/wiki/DOM_events .
Quick Tips :
1. you can prevent right click on page using
In this article we will see how that functionality can be achieved in few lines of code using jquery.
Step 1. Create an aspx page with following markup.
<body>
<form id="form1" runat="server">
<div>
<div align="center">
<fieldset style="width:400px;height:180px;">
<table cellpadding="3" cellspacing="3" border="0">
<tr>
<td colspan="2" class="header">Login</td>
</tr>
<tr>
<td> <asp:Label id="lblUser"
Text="UserName:
" runat="server"/>
</td>
<td> <asp:TextBox ID="txtUser"
Width="200px"
runat="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td><asp:Label id="lblPwd" Text="Password:"
runat="server"/></td>
<td><asp:TextBox ID="txtPwd" Width="200px" runat="server"
TextMode="Password"></asp:TextBox></td>
</tr>
<tr><td></td><td><asp:Button ID="btnSubmit" runat="server"
Text="SUBMIT" />
<asp:Button ID="btnReset" runat="server" Text="RESET" /></td></tr>
</table>
</fieldset>
</div>
</form>
</body>
Step 2. In the $(document).ready(function(){}) of jquery inject the following scritp(self explanatory).
<script language="javascript" type="text/javascript">
$(document).ready(function() {
//Step 1.
get that textbox in which you want to prevent the cut copy
//paste
operation and use the jquery's bind event to bind event
//handler
for cut copy and paste dom event
$('#<%=txtPwd.ClientID
%>').bind('cut copy paste', function(e) {
e.preventDefault();
alert("cut
/ copy / paste disabled in this textbox!");
}); //jquery
bind event ends here
});
//$(document).ready ends here...
</script>
Note : you can get more info on .bind from www.jquery.com and DOM event list from http://en.wikipedia.org/wiki/DOM_events .
Quick Tips :
1. you can prevent right click on page using
//Prevent right
click to pop up contextmenu
$(this).bind('contextmenu', function(e)
{
e.preventDefault();
});
2. And if you want to disable the text selection in your web page then use following:
Replace "this" from any div id where you dont want selection otherwise leave this as it is, which will prevent selection in the entire page.
$(this).children().each(function(i, c) {
disableSelection(c);
});
function
disableSelection(target) {
//alert(target)
if
(typeof target.onselectstart != "undefined") //For
IE
target.onselectstart = function() { return false }
else if (typeof
target.style.MozUserSelect != "undefined")
//For Firefox
target.style.MozUserSelect
= "none"
else
//All other route (For Opera)
target.onmousedown = function() { return false }
target.style.cursor = "default"
}