Share Blog

Saturday, May 31, 2014

Disable Copy/Paste/Right Click using Javascript in ASP.NET TextBox

Description:-

In this Example we Explain that How to DISABLE Cut,copy,Paste option in TextBox or Disabled CTRL+C, CTRL+V and CTRL+X. in TextBox using Javascript
Due to some reasons or in many situation like don’t allow to copy Password from Password TextBox to Confirm Password TextBox or Copy Password from TextBox to Confirm Password TextBox as we all see in every web application so we restrict users to copy, paste and cut contents from TextBox by using CTRL+C, CTRL+V and CTRL+X. We can implement this functionality by using below methods.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
      <title>Disable Copy/Paste/Right Click using Javascript in ASP.NET TextBox</title>

    <script type="javascript">

        function DisableRightClick(event) {

            //For mouse right click

            if (event.button == 2) {

                alert("Right Click is Disabled....!");

            }

        }

        function DisableCtrlKey(e) {

            var code = (document.all) ? event.keyCode : e.which;

            var message = "Ctrl key  is disabled.......!";

            // look for CTRL key press

            if (parseInt(code) == 3) {

                alert(message);

                window.event.returnValue = false;

            }

        }

    </script>

  
    <style type="text/css">
        .auto-style1
        {
            width: 100%;
        }
        .auto-style2
        {
            width: 162px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label1" runat="server" Text="User Name"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" onmousedown="DisableRightClick(event)"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" onkeydown="return DisableCtrlKey(event) "></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label3" runat="server" Text="Confirm Password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" oncopy="return false" onpaste="return false" oncut="return false"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Button ID="Button1" runat="server" Text="Login" />
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>

</html>


Using JQuery  -------------

<script type="text/javascript">
        $(document).ready(function () {
            $('#TextBox1').bind('copy paste cut', function (e) {
                e.preventDefault(); //disable cut,copy,paste
                alert('cut,copy & paste options are disabled !!');
            });
        });


    </script>

No comments:

Post a Comment