disable ctrl-v (paste)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chirs

    disable ctrl-v (paste)

    I have a code to disable ctrl-v (paste) on the 2nd box. The problem
    is that when I type ctrl-v, the text shows, then disappear
    after I release ctrl-v. How can I make it not to show in the box. In
    VB, I can set keyascii=0 to kill the input. Is there a similar way to
    do it in JavaScript? Thanks a lot.

    <HTML><head></head><body>
    <form name="myForm">
    Password: <input type="text"><br >
    Comfirm: <input type="text" name="myText"
    onKeyUp = "fncKeyStop();" > </form>
    <script>
    function fncKeyStop(){
    if (window.event.c trlKey){
    if (window.event.k eyCode == 86) {
    document.myForm .myText.value = ""
    }
    }}
    </script></body></HTML>

    Chris
  • Michael Winter

    #2
    Re: disable ctrl-v (paste)

    chirs wrote on 28 Nov 2003:
    [color=blue]
    > I have a code to disable ctrl-v (paste) on the 2nd box. The
    > problem is that when I type ctrl-v, the text shows, then
    > disappear after I release ctrl-v. How can I make it not to show
    > in the box. In VB, I can set keyascii=0 to kill the input. Is
    > there a similar way to do it in JavaScript? Thanks a lot.
    >
    > <HTML><head></head><body>
    > <form name="myForm">
    > Password: <input type="text"><br >
    > Comfirm: <input type="text" name="myText"[/color]

    Typo: Comfirm -> Confirm
    ^ ^
    [color=blue]
    > onKeyUp = "fncKeyStop();" > </form>
    > <script>[/color]

    The type attribute is mandatory. This should read:

    <SCRIPT type="text/javascript">
    [color=blue]
    > function fncKeyStop(){
    > if (window.event.c trlKey){
    > if (window.event.k eyCode == 86) {
    > document.myForm .myText.value = ""[/color]

    You should access forms and form elements using their respective
    collections:

    document.forms['myForm'].elements['myText'].value = "";
    [color=blue]
    > }
    > }}
    > </script></body></HTML>[/color]

    Use both the onkeyup and onkeydown events. That way, it will fire
    when a key is pressed once, and when it's released (if held long
    enough for the keystroke to repeat).

    You should also change the function content to:

    function fncKeyStop() {
    // Check if the control key is pressed.
    // If the Netscape way won't work (event.modifier s is undefined),
    // try the IE way (event.ctrlKey)
    var ctrl = typeof event.modifiers == 'undefined' ?
    event.ctrlKey : event.modifiers & Event.CONTROL_M ASK;

    // Check if the 'V' key is pressed.
    // If the Netscape way won't work (event.which is undefined),
    // try the IE way (event.keyCode)
    var v = typeof event.which == 'undefined' ?
    event.keyCode == 86 : event.which == 86;

    // If the control and 'V' keys are pressed at the same time
    if ( ctrl && v ) {
    // ... discard the keystroke and clear the text box
    document.forms['myForm'].elements['myText'].value = '';
    return false;
    }
    return true;
    }

    ....and the intrinsic event bodies to:

    <INPUT ... onkeyup="return fncKeyStop()"
    onkeydown="retu rn fncKeyStop()">

    This works in Internet Explorer and Opera. It should hopefully work
    in Mozilla and Netscape too, but I can't test them.

    Hope that helps,

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.uk.invalid (remove ".invalid" to reply)

    Comment

    • Nige

      #3
      Re: disable ctrl-v (paste)

      In comp.lang.javas cript, chirs wrote:
      [color=blue]
      >I have a code to disable ctrl-v (paste) on the 2nd box.[/color]

      Presumably this is to stop users from pasting their email address having
      typed it once. If so, may I say how annoyed I would be as a user.


      --
      Nige

      Please replace YYYY with the current year
      ille quis mortem cum maximus ludos, vincat

      Comment

      • Fabian

        #4
        Re: disable ctrl-v (paste)

        Michael Winter hu kiteb:
        [color=blue]
        > type = content-type [p.53] [CI] [p.49]
        > This attribute specifies the scripting language of the element$BCT(B
        > contents and overrides the default scripting language. The[/color]
        =============== ============[color=blue]
        > scripting language is specified as a content type (e.g.,
        > "text/javascript"). Authors must supply a value for this attribute.
        > There is no default value for this attribute.[/color]
        =============== =============== =====

        I think this specification could do with some proof-reading if your
        interpretation is correct.
        [color=blue]
        > <META http-equiv="Content-Script-Type" content="text/javascript">[/color]

        Does this replace the TYPE attribute in the SCRIPT tag?

        --
        --
        Fabian
        Visit my website often and for long periods!


        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: disable ctrl-v (paste)

          "Fabian" <lajzar@hotmail .com> writes:
          [color=blue]
          > Michael Winter hu kiteb:
          >[color=green]
          >> type = content-type [p.53] [CI] [p.49]
          >> This attribute specifies the scripting language of the element
          >> contents and overrides the default scripting language. The[/color]
          > =============== ============[color=green]
          >> scripting language is specified as a content type (e.g.,
          >> "text/javascript"). Authors must supply a value for this attribute.
          >> There is no default value for this attribute.[/color]
          > =============== =============== =====
          >
          > I think this specification could do with some proof-reading if your
          > interpretation is correct.[/color]

          Not really.

          There is a "default scripting language" for a page. It can be set
          with the META tag shown below. It is the default scripting language
          for all scripts on the page.

          There is no default *value* for the script tag's type *attribute*.
          Furthermore, the type attribute is required, so in practice, the
          default scripting language doesn't apply to script elements.

          The "default scripting language" and the "type attribute" of script
          tags are completely independent. The latter has no default value.
          [color=blue][color=green]
          >> <META http-equiv="Content-Script-Type" content="text/javascript">[/color]
          >
          > Does this replace the TYPE attribute in the SCRIPT tag?[/color]

          No. It's not about script tags at all. Script tags require the type
          attriubte. The intrinsic event attributes (e.g., "onclick") have no
          type attribute, and they use the default scripting language of the
          page.

          Intrinsic events:
          <URL:http://www.w3.org/TR/html4/interact/scripts.html#ev ents>
          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Dr John Stockton

            #6
            Re: disable ctrl-v (paste)

            JRS: In article <lb8esvg00tdjva omuamde5l0h80rj 4qhbr@4ax.com>, seen in
            news:comp.lang. javascript, Nige <uYYYY@ntlworld .com> posted at Fri, 28
            Nov 2003 10:22:38 :-[color=blue]
            >In comp.lang.javas cript, chirs wrote:
            >[color=green]
            >>I have a code to disable ctrl-v (paste) on the 2nd box.[/color]
            >
            >Presumably this is to stop users from pasting their email address having
            >typed it once. If so, may I say how annoyed I would be as a user.[/color]

            It is a password confirm box, AFAICS, which means that using ^V rather
            defeats the object of having a confirmation.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            • Fabian

              #7
              Re: disable ctrl-v (paste)

              Dr John Stockton hu kiteb:
              [color=blue]
              > JRS: In article <lb8esvg00tdjva omuamde5l0h80rj 4qhbr@4ax.com>, seen in
              > news:comp.lang. javascript, Nige <uYYYY@ntlworld .com> posted at Fri, 28
              > Nov 2003 10:22:38 :-[color=green]
              >> In comp.lang.javas cript, chirs wrote:
              >>[color=darkred]
              >>> I have a code to disable ctrl-v (paste) on the 2nd box.[/color]
              >>
              >> Presumably this is to stop users from pasting their email address
              >> having typed it once. If so, may I say how annoyed I would be as a
              >> user.[/color]
              >
              > It is a password confirm box, AFAICS, which means that using ^V rather
              > defeats the object of having a confirmation.[/color]

              In which case, shouldnt the form object be of type=password? iirc, that
              type has cut and paste functions disabled as a 'security' measure.


              --
              --
              Fabian
              Visit my website often and for long periods!


              Comment

              • Michael Winter

                #8
                Re: disable ctrl-v (paste)

                Fabian wrote on 28 Nov 2003:
                [color=blue]
                > Michael Winter hu kiteb:
                >[color=green]
                >> type = content-type [p.53] [CI] [p.49]
                >> This attribute specifies the scripting language of the
                >> element$BCT(B contents and overrides the default scripting
                >> language. The[/color]
                > =============== ============[color=green]
                >> scripting language is specified as a content type (e.g.,
                >> "text/javascript"). Authors must supply a value for this
                >> attribute. There is no default value for this attribute.[/color]
                > =============== =============== =====
                >
                > I think this specification could do with some proof-reading if
                > your interpretation is correct.[/color]

                It's not an interpretation, that is quoted verbatim. The only edited
                sections are the descriptions of the src and defer attributes (which
                I removed completely).
                [color=blue][color=green]
                >> <META http-equiv="Content-Script-Type"
                >> content="text/javascript">[/color]
                >
                > Does this replace the TYPE attribute in the SCRIPT tag?[/color]

                The type attribute is required. That why the specification says (as
                you quoted!): "Authors must supply a value for this attribute." The
                Content-Script-Type META element or HTTP header is used when parsing
                intrinsic events. It is explained in section 18.2.2, Specifying the
                scripting language, of the HTML 4.01 specification.

                Mike

                --
                Michael Winter
                M.Winter@blueyo nder.co.uk.invalid (remove ".invalid" to reply)

                Comment

                • Dr John Stockton

                  #9
                  Re: disable ctrl-v (paste)

                  JRS: In article <bq8pkt$1v85b8$ 1@ID-174912.news.uni-berlin.de>, seen in
                  news:comp.lang. javascript, Fabian <lajzar@hotmail .com> posted at Sat, 29
                  Nov 2003 09:07:57 :-[color=blue]
                  >Dr John Stockton hu kiteb:[color=green]
                  >> It is a password confirm box, AFAICS, which means that using ^V rather
                  >> defeats the object of having a confirmation.[/color]
                  >
                  >In which case, shouldnt the form object be of type=password? iirc, that
                  >type has cut and paste functions disabled as a 'security' measure.[/color]

                  You do not RC! Copy and Cut are disabled, but Paste works, in my
                  browser, in a Password box. But it seems likely that it should be
                  type=password.

                  --
                  © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                  <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                  <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                  <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                  Comment

                  • David Leverton

                    #10
                    Re: disable ctrl-v (paste)

                    chirs wrote:[color=blue]
                    > I have a code to disable ctrl-v (paste) on the 2nd box.[/color]
                    [snip]

                    How do you know that Ctrl-V is the shortcut for Paste? You could be
                    disabling some other completely different and important feature of the
                    browser.

                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: disable ctrl-v (paste)

                      David Leverton <u01drl3@abdn.a c.uk> writes:
                      [color=blue]
                      > chirs wrote:[color=green]
                      >> I have a code to disable ctrl-v (paste) on the 2nd box.[/color]
                      > [snip]
                      >
                      > How do you know that Ctrl-V is the shortcut for Paste? You could be
                      > disabling some other completely different and important feature of the
                      > browser.[/color]

                      E.g., it probably also disables Ctrl-Alt-V, which has a function in
                      Opera. Not one that you would probably want the user to use, though,
                      as it sends the page to an HTML validator :)

                      /L
                      --
                      Lasse Reichstein Nielsen - lrn@hotpop.com
                      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      Working...