How to detect ctrl+v ,Ctrl+c using Javascript?
i need to restrict pasting in my textareas,end user should not copy and paste the content, user should only type text in text area. how to achive this.
Thanks in advance.
How to detect ctrl+v ,Ctrl+c using Javascript?
i need to restrict pasting in my textareas,end user should not copy and paste the content, user should only type text in text area. how to achive this.
Thanks in advance.
You can listen to the keypress event, and halt the default event (entering the text) if it matches the specific keycodes
Never do this. The world is lucky that it's difficult to do. It's the user's computer, and it is a security, choice and usability issue if you intrude into their choice. Go away.
I wrote a jQuery plugin, which catches keystrokes. It can be used to enable multiple language script input in html forms without the OS (except the fonts). Its about 300 lines of code, maybe you like to take a look:
Generally, be careful with such kind of alterations. I wrote the plugin for a client because other solutions weren't available.
function noCopyMouse(e) {
if (event.button == 2 || event.button == 3) {
alert('You are prompted to type this twice for a reason!');
return false;
}
return true;
}
function noCopyKey(e) {
var forbiddenKeys = new Array('c','x','v');
var isCtrl;
if(window.event) {
if(window.event.ctrlKey)
isCtrl = true;
else
isCtrl = false;
}
else {
if(e.ctrlKey)
isCtrl = true;
else
isCtrl = false;
}
if(isCtrl) {
for(i=0; iif(forbiddenKeys[i] == String.fromCharCode(window.event.keyCode).toLowerCase()) {
alert('You are prompted to type this twice for a reason!');
return false;
}
}
}
return true;
}
And now to reference those methods in the textboxes that you wish to restrict:
<input name="txtTest" id="txtTest" type="textbox" onmousedown="javascript:return noCopyMouse(event);" onkeykown="javascript:return noCopyKey(event);" />
There is some ways to prevent it.
However the user will be always able to turn the javascript off or just look on the source code of the page.
Some examples (require jQuery)
/**
* Stop every keystroke with ctrl key pressed
*/
$(".textbox").keydown(function(){
if (event.ctrlKey==true) {
return false;
}
});
/**
* Clear all data of clipboard on focus
*/
$(".textbox").focus(function(){
if ( window.clipboardData ) {
window.clipboardData.setData('text','');
}
});
/**
* Block the paste event
*/
$(".textbox").bind('paste',function(e){return false;});
Edit: How Tim Down said, this functions are all browser dependents.
I just did this out of interest. I agree it's not the right thing to do, but I think it should be the op's decision... Also the code could easily be extended to add functionality, rather than take it away (like a more advanced clipboard, or Ctrl+s triggering a server-side save).
$(document).ready(function()
{
var ctrlDown = false;
var ctrlKey = 17, vKey = 86, cKey = 67;
$(document).keydown(function(e)
{
if (e.keyCode == ctrlKey) ctrlDown = true;
}).keyup(function(e)
{
if (e.keyCode == ctrlKey) ctrlDown = false;
});
$(".no-copy-paste").keydown(function(e)
{
if (ctrlDown && (e.keyCode == vKey || e.keyCode == cKey)) return false;
});
});
Also just to clarify, this script requires the jQuery library.
EDIT: removed 3 redundant lines (involving e.which) thanks to Tim Down's suggestion (see comments)
i already have your problem and i solved it by the following code .. that accept only numbers
$('#<%= mobileTextBox.ClientID %>').keydown(function(e) {
///// e.which Values
// 8 : BackSpace , 46 : Delete , 37 : Left , 39 : Rigth , 144: Num Lock
if (e.which != 8 && e.which != 46 && e.which != 37 && e.which != 39 && e.which != 144
&& (e.which < 96 || e.which > 105 )) {
return false;
}
});
you can detect ctrl
id e.which == 17
There's another way of doing this: onpaste
, oncopy
and oncut
events can be registered and cancelled in IE, Firefox, Chrome, Safari (with some minor problems), the only major browser that doesn't allow cancelling these events is Opera.
As you can see in my other answer intercepting ctrl+v
and ctrl+c
comes with many side effects, and it still doesn't prevent users from pasting using the Firefox Edit
menu etc.
function disable_cutcopypaste(e) {
var fn = function(evt) {
// IE-specific lines
evt = evt||window.event
evt.returnValue = false
// Other browser support
if (evt.preventDefault)
evt.preventDefault()
return false
}
e.onbeforepaste = e.onbeforecopy = e.onbeforecut = fn
e.onpaste = e.oncopy = e.oncut = fn
}
Safari still has some minor problems with this method (it clears the clipboard in place of cut/copy when preventing default) but that bug appears to have been fixed in Chrome now.
See also: http://www.quirksmode.org/dom/events/cutcopypaste.html and the associated test page http://www.quirksmode.org/dom/events/tests/cutcopypaste.html for more information.
Some good uses I can think of for this is for foreign language inputs (like onscreen keyboards, input methods for CJK languages), flashcards or kiosks as other users have noted.
Please treat your users with civility, and use it only for special cases :-)