Disable textbox cut copy paste in asp.net and asp.net mvc using javascript,jquery
In some scenarios, we don't want user to be able to cut or copy the value of text box and we also want to restrict user to not be able to paste value from clipboard in text box.
User have couple of options, he can use short cut keys , for example Ctrl + C for copying and Ctrl + V , or user can right click inside the control and can use use context menu buttons which are mostly Cut, Copy and Paste available for doing desired operation on control.
One way is to write java-script events for cut,copy and paste so that whenever user do one of these operations on control we handle them to override the default implementation of these commands.
In asp.net mvc we can use the htmlAttributes parameter of Html Helper method do it.
Here is an example code using javacript:
Here is the Live DEMO Fiddle of it.
We can do the same using jquery as well this way:
and in jquery :
You can do it same way in asp.net web forms as well, for example:
User have couple of options, he can use short cut keys , for example Ctrl + C for copying and Ctrl + V , or user can right click inside the control and can use use context menu buttons which are mostly Cut, Copy and Paste available for doing desired operation on control.
One way is to write java-script events for cut,copy and paste so that whenever user do one of these operations on control we handle them to override the default implementation of these commands.
In asp.net mvc we can use the htmlAttributes parameter of Html Helper method do it.
Disabling Using JavaScript:
Here is an example code using javacript:
@Html.TextBoxFor(model => model.Question, new { @class="form-control", oncut="return false" oncopy="return false", onpaste="return false" } )
Here is the Live DEMO Fiddle of it.
Disabling Using JQuery:
We can do the same using jquery as well this way:
@Html.TextBoxFor(model => model.Question, new { @class="form-control", id="txtQuestion" oncut="return false" oncopy="return false", onpaste="return false" } )
and in jquery :
$("#txtQuestion").on("cut copy paste",function(e) { e.preventDefault(); // it will cause to stop default behaviour ofcommand // or we can use return false as well, as we did in js }
You can do it same way in asp.net web forms as well, for example:
<asp:TextBox ID="txtQuestion" runat="server" CssClass="form-control" oncut="return false" oncopy="return false" onpaste="return false"> </asp:TextBox>