Prevent paste copying using jquery

You must prohibit copying to a text field using jquery. How to implement it?

<table>
   <tr>
     <h:inputlabel value="Actual"></h:inputlabel>
      <td>
        <h:inputtext id="Actual" styleClass="input-tex" value="#bean.customer"></h:inputtext>
      <td>
   </tr>
<table>
+5
source share
3 answers

Go to Disable cut, copy and paste for textbox using jQuery

$(document).ready(function(){
  $('#Actual').bind("cut copy paste",function(e) {
      e.preventDefault();
  });
});

Note. Opera did not support cutting, copying, and pasting events up to version 12.10

+11
source

This is the most “official” way to do this with jQuery.

$(document).ready(function () {
    var ambit = $(document);

    // Disable Cut + Copy + Paste (input)
    ambit.on('copy paste cut', function (e) {
        e.preventDefault(); //disable cut,copy,paste
        return false;
    });
});

However, it only works and, as I read, is not supported on some versions of Opera. Anything outside the entrance is allowed to copy.

If you want to completely disable paranoic mode: on, you can use this method:

$(document).ready(function () {
    var ambit = $(document);

    // Disable Cut + Copy + Paste (input)
    ambit.on('copy paste cut', function (e) {
        e.preventDefault(); //disable cut,copy,paste
        return false;
    });

    // Disable Cut + Copy + Paste and Browser Admin Tools (all document)
    ambit.keydown(function (e) {
        var forbiddenCtrlKeys = new Array('c', 'x', 'v', 'ins', 'u');
        var forbiddenShiftKeys = new Array('del', 'ins', 'f2', 'f4', 'f7');
        var forbiddenCtrlShiftKeys = new Array('k', 'i', 'm', 's', 'j');
        var keyCode = (e.keyCode) ? e.keyCode : e.which;

        var isCtrl, isShift;
        isCtrl = e.ctrlKey;
        isShift = e.ctrlShift;

        string = getKeyCodeString(keyCode);

        if (string == 'f12')
        {
            e.preventDefault();
            return false;
        }

        if (isCtrl && !isShift) {
            for (i = 0; i < forbiddenCtrlKeys.length; i++) {
                if (forbiddenCtrlKeys[i] == string) {
                    e.preventDefault();
                    return false;
                }
            }
        }

        if (!isCtrl && isShift) {
            for (i = 0; i < forbiddenShiftKeys.length; i++) {
                if (forbiddenShiftKeys[i] == string) {
                    e.preventDefault();
                    return false;
                }
            }
        }

        if (isCtrl && isShift) {
            for (i = 0; i < forbiddenCtrlShiftKeys.length; i++) {
                if (forbiddenCtrlShiftKeys[i] == string) {
                    e.preventDefault();
                    return false;
                }
            }
        }

        return true;
    });

    var getKeyCodeString = function(keyCode)
    {
        var string;
        switch (keyCode) {
            case 45:
                string = 'ins'; break;
            case 46:
                string = 'del'; break;
            case 113:
                string = 'f2'; break;
            case 115:
                string = 'f4'; break;
            case 118:
                string = 'f7'; break;
            case 123:
                string = 'f12'; break;
            default:
                string = String.fromCharCode(keyCode);
                break;
        }
        return string.toLowerCase();
    }
});

?

$(document).ready(function () {
    var ambit = $(document);

    // Disable Contextual Menu
    ambit.on('contextmenu', function (e) {
        e.preventDefault();
        return false;
    });

?

$(document).ready(function () {
    var ambit = $(document);

    // Disable Tap and Hold (jQuery Mobile)
    ambit.on('taphold', function (e) {
        e.preventDefault();
        return false;
    });
});

, ! !

+1

Since JQuery 1.9 onward live event is not supported, we can use "on" for the same purpose.

  $('#Actual').on("cut copy paste", function (e) {
      e.preventDefault();
  });
0
source

All Articles