/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu

Friday, April 6, 2012

Disable and Restrict Copy Cut and Paste from on a textbox


You will need two javascript functions for this:


function noCopyMouse(e){ 
  var isRight = (e.button) ? (e.button == 2) : (e.which == 3); 
  if(isRight){ 
    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 keyCode = (e.keyCode) ? e.keyCode : e.which; 
var isCtrl;
  if(window.event) 
  isCtrl = e.ctrlKey 
  else 
  isCtrl = (window.Event) ? ((e.modifiers & Event.CTRL_MASK)
  == Event.CTRL_MASK):false
 
  if(isCtrl){
    for(i = 0; i < forbiddenKeys.length; i++) { 
     if(forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()){
       alert('You are prompted to type this twice for a reason!');
       return false; 
     } 
    } 
  } 
  return true; 
}


And a wee bit of code-behind to handle the two events for the textbox(es):
Textbox1.Attributes.Add("onmousedown", "return noCopyMouse(event);");
Textbox1.Attributes.Add("onkeydown", "return   noCopyKey(event);");
_____________________________________________________________________
 
Realize that the user can still use Edit|Copy and Edit|Paste in their browser menu.  There's nothing you can do to prevent that, unfortunately.
Hope this helps!  Don't forget to mark the most helpful post(s) as Answer for the sake of future readers.  Thanks!

No comments:

Post a Comment

My Blog List

  • काश - काश मुझे भी पीने की आदत होती,मैं कब का मुर्दा हो गया होता। छुटकारा मिलता आज के आतंकवाद से, किसी संतान भूमि में सो गया होता। मेरा एतबार कौन करेगा, मैंने मुर...
    3 months ago
  • काश - काश मुझे भी पीने की आदत होती,मैं कब का मुर्दा हो गया होता। छुटकारा मिलता आज के आतंकवाद से, किसी शमशान भूमि में सो गया होता। मेरा एतबार कौन करेगा, मैंने मुर...
    3 months ago
  • Kumaon University Nainital B.Ed entrance exam test result 2012 - कुमाऊँ विश्वविधालय, नैनीताल (उत्तराखण्ड)
    10 years ago