Disable cut/copy/paste operations on an ASP.NET Textbox

1 comment

In this section, let's see how to disable cut/copy/paste operations on an ASP.NET Textbox. When requesting sensitive information from the end user such as passwords, credit card details, and transaction pin numbers used in Internet banking, etc. it is preferred that the web user keys in the characters manually rather than pasting from the clipboard.

Add a new web form DisableCopy.aspx to the current project.

Now add following simple password change form:
<form id="form1" runat="server">
    <div align="center">
        <fieldset style="width: 400px; height: 180px;">
            <table cellpadding="3" cellspacing="3" border="0">
                <tr>
                    <td colspan="2" class="header">
                        CHANGE PASSWORD
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblCurrentPwd" Text="Current Password:" runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="txtCurrentPwd" Width="200px" runat="server" TextMode="Password"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblNewPwd" Text="New Password:" runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="txtNewPwd" Width="200px" runat="server" TextMode="Password"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label id="lblConfirmNewPwd" runat="server">
                            Confirm New Password:</label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtConfirmNewPwd" 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>

Now add following jQuery -
<script type="text/javascript">
$(document).ready(function() {
$('#<%=txtNewPwd.ClientID%>').bind('cut copy paste', function(e) {
e.preventDefault();
alert("Cut / Copy / Paste disabled in this textbox");
});
$('#<%=txtConfirmNewPwd.ClientID%>').bind('cut copy paste', function(e) {
e.preventDefault();
alert("Cut / Copy / Paste disabled in this textbox too!");
});
});
</script>

Run the web page. Try cut/copy/paste operations on the New Password and Confirm New Password fields. The page will throw the information message.

1 comment:

  1. Your article helps to work on asp. net projects only, Here is a jquery tut, this will work on html, php, asp sites.,

    http://kvcodes.com/2014/03/disabling-textbox-cut-copy-and-paste-operations/

    ReplyDelete