Friday, 30 May 2014

Disable cut, copy and paste in textbox using jquery, javascript





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 or Jquery.

Due to some reasons or in many situation like don’t allow to copy Email from Email TextBox to Confirm Email 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.

You can perform this operation in many ways like using javascript or jquery as you preffered.here we explain all the method to restrict the user to do cut,copy,paste in .aspx TextBox in Asp.Net.

To Show Example of How to Upload File in MVC Application then click here upload Image and bind to Gridview in MVC

How to upload File and Fetch file from SqlServer and bind to Gridview fetch file from Sqlserver and bind to Gridview

Using Javascript :-

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">

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

    <script language="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) == 17) {

                alert(message);

                window.event.returnValue = false;

            }

        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <strong>Here in This TextBox Right click disabled</strong> textbox<br />
        <asp:TextBox ID="txtCopy" runat="server" onmousedown="DisableRightClick(event)">

        </asp:TextBox>
        <br />
        <br />
        <strong>Here in this Textbox Ctrl key </strong>disabled<br />
        <asp:TextBox ID="txtctrl" runat="server" onkeydown="return DisableCtrlKey(event)">

        </asp:TextBox>
        <br />
        <br />
        Here below disable<strong> Cut, Copy and Paste </strong>in textbox<br />
        <br />
        <asp:TextBox ID="txtccp" runat="server" oncopy="return false" onpaste="return false"
            oncut="return false">

        </asp:TextBox>
    </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>

0 comments:

Post a Comment