Monday, 8 February 2016

To prevent Cut, Copy and Past in ASP.NET using Jquery functions .




1.  If you don't want to alert any message then just put  below property in your control to prevent Copy, Cut and Past 

oncopy="return false"

onpaste="return false"
oncut="return false"

Example:-
<asp:TextBox ID="txt" runat="server" oncopy="return false" onpaste="return false" oncut="return false"></asp:TextBox>




2.In JavaScript you can use below code

<script language="javascript" type="text/javascript">
     function DisableCopyPaste(e) {
         // Message to display
         var message = "Cntrl key/ Right Click Option disabled";
         // check mouse right click or Ctrl key press
         var kCode = event.keyCode || e.charCode;
         //FF and Safari use e.charCode, while IE use e.keyCode
         if (kCode == 17 || kCode == 2) {
             alert(message);
             return false;
         }
     }
</script>


 <asp:TextBox ID="txtId" runat="server" onKeyDown="return DisableCopyPaste(event)"onMouseDown="return DisableCopyPaste (event)"></asp:TextBox>


3.In JQuery you can use below code


<script lang="javascript" type="text/javascript">

         $(document).ready(function () {
             $('#<%=txt.ClientID%>').bind('copy paste cut', function (e) {
                 e.preventDefault(); //disable cut,copy,paste
                 alert('cut,copy & paste options are disabled !!');
             });
         });
       </script>

 <asp:TextBox ID="txt" runat="server"></asp:TextBox>


In the .bind(param1, param2) function first parameter you have to mention the name of property on which you want to execute function and in second parameter you write that function.



4. To check existence of substring in the string

    var str = $('#<%=txt.ClientID%>').val();
                 if (str.indexOf("Hello") >= 0) {
                     alert('Contains hello');
                 }


5. To change a string from Lower to Upper and Upper to Lower case

    var str = $('#<%=txt.ClientID%>').val();

                 alert(str.toLowerCase());

    var str = $('#<%=txt.ClientID%>').val();
                 alert(str.toUpperCase());


6. To check a numeric value 

    var str = $('#<%=txt.ClientID%>').val();

              $.isNumeric(str);

  Note- Its return boolean value.
  For numeric       - true
  For None numeric  - false





No comments:

Post a Comment