While working
sometimes we need to restrict users to cut copy and paste contents from TextBox.
It’s quite easy to do this using either JavaScript or jQuery.
·
For
JavaScript we can disable by adding attribute to TextBox.
·
For
jQuery we need is to bind cut, copy and paste function to Textbox and stop the
current action with the ‘preventDefault‘ method.
Code to
disable cut/copy/paste:
//Disable using JavaScript:
<asp:TextBox ID="txtName" runat="server" oncopy="return false" onpaste="return false" oncut="return false"></asp:TextBox>
//Disable with
the help of bind method
$(document).ready(function () {
$('#txtName').bind("cut copy paste", function (e) {
e.preventDefault();
});
});
Sometime there may be issue with
bind method, in such cases user can go for live method:
//Disable with
the help of live method
$(document).ready(function () {
$('#txtName').live("cut copy paste", function (e) {
e.preventDefault();
});
});
|
Comments
Post a Comment