Prevent Cut, Copy and Paste operations in ASP.NET using JQuery

Here you can see how to prevent user to do cut, copy and paste operations on some of the textbox in an ASP.Net website using JQuery.
  • 2014

We can prevent user to do cut, copy and paste operations on some of the textbox in an ASP.Net website using JQuery. Most probably restriction can be made on Email and Confirm Email field in order to force user to type themselves.

Here we provide three textboxes for user as their name, email id and password and block all of the three boxes for cut, copy and paste. So, user have to type information by themselves.

Code Snippets

<html>
<
head>
<
title>Example</title>
<script src="jquery.js" type="text/javascript"></script>    
   <script type="text/javascript">
       $(function () {
          $("#<% =txtUserName.ClientID %>,#<% =txtEmailId.ClientID%>,#<% =txtPassword.ClientID%>").bind("cut
copy paste"
, function (event) {
              event.preventDefault();
              alert('You cannot ' + event.type + ' text!'); 
           });
       });
   </script>
   <style type="text/css">
      .style1
      {
         color: #B30000;
      }
   </style>
</head>
<
body>
  <form id="form1" runat="server">
    <div style="border-style: none; font-family: Verdana; font-size: x-small; background-color: #FFF0F0;">    
        <strong style="border-style: ridge; font-size: small;"><span class="style1">Type 
        the information in the required field:   
     </
span><br />
        </strong>
           <br /><br />
        <strong style="font-size: x-small">User Name</strong>
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
           <br /><br />
        <b>mail Id </b><asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox>
 
          <br /><br />
        <strong>Password</strong> <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
           <br /><br />
         <asp:Button ID="Button1" runat="server" Font-Names="Verdana" Font-Size="Small" 
            style="font-weight: 700" Text="Login" Width="80px" />
           <br />
  </form>
</
body>
</html>

Output
TypeOnlyText1.gif

Now a message will be display when we try to perform any cut, copy and paste operation on the textbox as shown below: 

TypeOnlyText2.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.