To Avoid Copy/Paste - Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • L Gopi
    New Member
    • Sep 2006
    • 5

    To Avoid Copy/Paste - Javascript

    Hello

    I have a requirement that paste (CTRL + V) needs to be disabled in some textboxes on my html page.

    Can anyone suggest any solution for this please... One constrain is that, the solution should work with different browsers...

    Thanks in advance...
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    have a look at the following example:

    [HTML]<html>
    <head>
    <script type="text/javascript">
    function key_pressed(e) {
    var evt = typeof window.event != 'undefined' ? window.event : e;
    var val = evt.charCode;

    if (evt.ctrlKey && val == 118) {
    if (evt.stopPropag ation) {
    evt.stopPropaga tion();
    } else {
    evt.cancleBubbl e = true;
    evt.returnValue = false;
    }
    }
    }
    </script>
    </head>
    <body>
    <input type="text" id="foo" name="bar" onkeydown="key_ pressed(event); "/>
    </body>
    </html>
    [/HTML]
    may be that helps? :)

    kind regards

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      hi ...

      i tested the code right here on a windows-machine (the last one worked for a FF on my macbook) ... here's the windows version:

      [HTML]<html>
      <head>
      <script type="text/javascript">
      function key_pressed(e) {
      var evt = typeof window.event != 'undefined' ? window.event : e;
      var val = evt.keyCode == 86;

      if (evt.ctrlKey && val) {
      if (evt.stopPropag ation) {
      evt.preventDefa ult();
      evt.stopPropaga tion();
      } else {
      evt.cancleBubbl e = true;
      evt.returnValue = false;
      }
      }
      }
      </script>
      </head>
      <body>
      <input type="text" id="foo" name="bar" onkeydown="key_ pressed(event); "/>
      </body>
      </html>
      [/HTML]
      so as you can see ... we not only had to have to identify browsers but the os too ... what about the context-menu? ... menubar of the browser? ... so you cannot really suppress this action ... but may be in case you have to ... then the examples might be a start ... :)

      kind regards

      Comment

      Working...