faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 7 people (71%) answered Yes
Recently 5 of 7 people (71%) answered Yes

Entry

Can I protect an input field by pasting (Ctrl-V) but the user can write in text/input field

Apr 8th, 2008 18:28
ha mo, Dave Clark, Kashif Zaidi,


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Kashif,
     The likely asnwer is, No -- but I haven't tried it.  The reason 
is because that is an operating-system-related key sequence.  There 
may be a security setting in your (or the user's) browser to disable 
paste operations.  However, you might give the following a try:
function NoPaste(evt)
{
  var mykey, ctrl, shift;
  if (evt) {
    mykey = evt.which;
    ctrl = (evt.modifiers & Event.CONTROL_MASK) ? true : false;
    shift = (evt.modifiers & Event.SHIFT_MASK) ? true : false;
  } else {
    mykey = window.event.keyCode;
    ctrl = window.event.ctrlKey;
    shift = event.shiftKey;
  }
  if((ctrl && mykey == 86)   // Ctrl-v
  || (shift && mykey == 45)) // Shift-Insert
    return false;       
  return true;
}
<input type="text" ...etc... onkeydown="return NoPaste()">
Take care,
Dave Clark
www.DaveClarkConsulting.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com