View Full Version : Disable Ctrl-V keystroke
dominicall
11-07-2002, 09:45 AM
Is there a way using javascript to disable the 'Ctrl-V' (paste) keystroke on a page.
Am using an iframe rich text editor and want to prevent people using Ctrl-V instead of the paste button.
I've built a function which removes all formatting tags from the source text when the paste button in the rich text editor is used and want to make sure that people use the paste button.
Thanks
Dominic :D
dominicall
11-07-2002, 11:46 AM
Don't worry - found a solution
:D
Dominic
piglet
11-07-2002, 11:49 AM
Hi,
AFAIK you can't do this - it's a real pain. You just have to re-validate the input onblur/onsubmit.
It's amazing how many places this is a problem - I recently had this problem with Excel. One of our users wanted a complex validation setup on their spreadsheet, and it turns out that Excel doesn't do validation when you paste data - only when you type it....:mad:
You found a solution? What is it?
dominicall
11-07-2002, 12:09 PM
I've attached the 'test' file I just created - note the test form only uses a text area for this - the reason I need to use this is that on the live form I'm using an iframe rich text editor which will pick up all HTML/Word formatting if you paste into it using Ctrl-V or the paste command from the right-click context menu/edit,paste from the main menu.
I found a couple of places on the web with this info.
I've also disabled the 'right mouse key' button on the keyboard and also the right mouse click.
It still won't stop anyone using the Edit,Paste from the browser menu (still trying to work that one out - but not sure it can be done at all).
It's probably enough though with the right alert message to encourage users to use the 'Paste' or 'Paste from Word' buttons in the form itself.
The 'Paste from Word' function is...
function pasteWord()
{
addsale2.box.focus();
addsale2.box.select();
document.execCommand('delete');
document.execCommand('paste');
addsale2.box.select();
document.execCommand('RemoveFormat');
document.execCommand('cut');
cleanFromWord()
idContent.focus();
idContent.document.execCommand('paste');
}
function cleanFromWord()
{
for (var i = 0; i < addsale2.box.all.length; i++)
{
addsale2.box.all[i].removeAttribute("className","",0);
addsale2.box.all[i].removeAttribute("style","",0);
}
var sHTML = addsale2.box.innerText;
sHTML = sHTML.replace(/<[^>]*>/g, "");
sHTML = sHTML.replace(/<\?xml:.*?\/>/g, "");
sHTML = sHTML.replace(/<o:p> <\/o:p>/g, "");
sHTML = sHTML.replace(/o:/g, "");
sHTML = sHTML.replace(/<st1:.*?>/g, "");
return sHTML;
}
If you want an explanation of how this works let me know - I've been a bit thorough with it - but that's just me...
Dominic
piglet
11-07-2002, 12:20 PM
Ah...
Yes - execCommand will do the trick, but it's "Internet Explorer only".
I personally don't use IE any more if I can avoid it...I use Mozilla or Phoenix. :) btw Opera doesn't let you disable the right mouse button...
dominicall
11-07-2002, 12:25 PM
LOL - yeah I know...
The iframe rich text editor only works with IE as well - I use a browser detect on the page to show either the iframe rich text editor for IE users or a normal textarea for everyone else.
Since most people will be using IE anyway I needed to have the functions and disable righ-click/Ctrl-V for them - anyone using another browser will be pasting into a textarea so I don't need to use the functions.
:D
Dominic
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.