faqts : Computers : Programming : Languages : JavaScript : Forms : TextAreas/TextFields

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

133 of 176 people (76%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

how to disable ctrl c and ctrl v in a text area, but still allow user to type in text box

Dec 3rd, 2001 03:33
Dan, Sherry Jensen,


For this I use onKeyDown event. See the following code
that disables CTRL C and CTRL V.
Note that this is not enough to prevent COPY/PASTE
in a text field, since the user can use CTRL INS, SHIFT INS
or simply use the context menu (right click on text field).
<html>
<head>
<script language="javascript">
function onKeyDown() {
  // current pressed key
  var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
  if (event.ctrlKey && (pressedKey == "c" || 
                        pressedKey == "v")) {
    // disable key press porcessing
    event.returnValue = false;
  }
} // onKeyDown
</script>
</head>
<body>
<form name="aForm">
<input type="text" name="aText" onkeydown = "onKeyDown()">
</form>
</body
</html>