Reputation: 1105
Need to prevent copy paste in a textbox 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>
Upvotes: 6
Views: 17737
Reputation: 8142
It's the most and 'official' way to do it 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 on and as I read isn't supported on some Opera versions. Everything outside of an input is allowed to be copied.
If what you want disable completely, 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();
}
});
And what about the contextual menu?
$(document).ready(function () {
var ambit = $(document);
// Disable Contextual Menu
ambit.on('contextmenu', function (e) {
e.preventDefault();
return false;
});
And what about mobile?
$(document).ready(function () {
var ambit = $(document);
// Disable Tap and Hold (jQuery Mobile)
ambit.on('taphold', function (e) {
e.preventDefault();
return false;
});
});
Hope it helps! Corrections and improvements are Welcome!
Upvotes: 2
Reputation: 48
As Jquery 1.9 onward live event is not supported, we can use "on" for same purpose.
$('#Actual').on("cut copy paste", function (e) {
e.preventDefault();
});
Upvotes: 0
Reputation: 17194
Here to go: Disable Cut, Copy and Paste function for textbox using jQuery
$(document).ready(function(){
$('#Actual').bind("cut copy paste",function(e) {
e.preventDefault();
});
});
Note: Opera didn't support cut, copy and paste events before version 12.10
Upvotes: 13