Preventing user from copy/paste option

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Archanak
    New Member
    • Sep 2006
    • 79

    Preventing user from copy/paste option

    Hi,

    I have a text box and should allow user to type the text but not to allow them to copy/paste or right click.

    Here is the code.
    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script language="javascript">
    function whichButton(event)
    {
    if (event.button==2)//RIGHT CLICK
    {
    alert("Not Allow Right Click!");
    }
    
    }
    function noCTRL(e)
    {
    var code = (document.all) ? event.keyCode:e.which;
    
    var msg = "Sorry, this functionality is disabled.";
    if (parseInt(code)==17) //CTRL
    {
    alert(msg);
    window.event.returnValue = false;
    }
    }
    </script>
    </head>
    <body>
    <form method="">
    <strong>Not Allow Paste </strong><BR>
    <input type="text" value="" onMouseDown="whichButton(event)" onKeyDown="return noCTRL(event)"/>
    </form>
    </body>
    </html>
    It works properly in IE and in mozilla also but in mozilla when right click option is used it will display the message saying that "Not allow right click option" but the menu will also displayed (i.e cut,copy,select all) and it allows to paste also.

    How can this be avoided?

    Any suggestions!!

    with regards
    Archana
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    The first question to ask yourself is why you need this.

    Rarely is it a good idea. In fact, some browsers allow users to prevent JavaScript code itself from preventing certain expected browser functionality.

    Comment

    • Archanak
      New Member
      • Sep 2006
      • 79

      #3
      Originally posted by acoder
      The first question to ask yourself is why you need this.

      Rarely is it a good idea. In fact, some browsers allow users to prevent JavaScript code itself from preventing certain expected browser functionality.
      Hi,

      I need this because we are using email option so i want user to type there email id .They should not copy/paste there id's they should only type it. For that reason i should solve this problem!!

      Any idea???

      with regards
      Archana

      Comment

      • rnd me
        Recognized Expert Contributor
        • Jun 2007
        • 427

        #4
        human typing vastly increases the chance for error.
        the form take longer to fill out.
        these types of restrictions can make the page less accessible to disabled users.
        doing so will also anger a large chuck of regular users.
        it also does nothing to penalize someone who has javascript disabled, how fair is that?

        what in your script prevents <shift>+<insert > or a menu key from pasting it in?

        i beg you to ask yourself why you are going down this path in the first place.
        clicking 'submit' can create a legally binding contract.

        there is not a good enough reason to do so.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          If it's for just a second confirmatory email field, this thread may help (if you insist).

          Comment

          • Archanak
            New Member
            • Sep 2006
            • 79

            #6
            Originally posted by rnd me
            human typing vastly increases the chance for error.
            the form take longer to fill out.
            these types of restrictions can make the page less accessible to disabled users.
            doing so will also anger a large chuck of regular users.
            it also does nothing to penalize someone who has javascript disabled, how fair is that?

            what in your script prevents <shift>+<insert > or a menu key from pasting it in?

            i beg you to ask yourself why you are going down this path in the first place.
            clicking 'submit' can create a legally binding contract.

            there is not a good enough reason to do so.
            Hi,

            Thanks for your reply!!!

            With regards
            Archana

            Comment

            • Archanak
              New Member
              • Sep 2006
              • 79

              #7
              Originally posted by acoder
              If it's for just a second confirmatory email field, this thread may help (if you insist).
              Hi,

              Ya sure i will look into that!!!

              Thanks for your reply!!!

              With regards
              Archana

              Comment

              • Rsmastermind
                New Member
                • Sep 2008
                • 93

                #8
                Both of my senior friends are correct but that is what the buisness concern.If someone wants to disable the right click not for the text field but for the pictures then the logic what my seniors has given is not correct.

                This is about the technical concept what we are dealing .Right now for Archanak she wants to do it for the text field and she is getting some problem wth the mozila explorer so instead of talking about the business we should tell her about why the problem is.Isn't it my friends?

                So, technically if you want to disable the right click of the mouse for the whole document then oncontextmenu is helpful.
                But for the different tags you can't use this .So what you had done is ok with IE.

                For mozila Add this piece of code where e is the event.As Mozila does not behave same as IE in context of script handling.
                Code:
                function ffdisable(e)
                {
                if (e.button==2)
                  { //firefox disable
                  e.preventDefault();
                  e.stopPropagation();
                  alert(message);
                  return false;
                  }
                }

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Not quite. I understood what Achana asked. Pictures don't make a difference because the logic is inherently flawed. Trying to prevent the user from copy/pasting creates more problems than it solves unless you have some form of control over the user base, you use it sparingly and wisely and there are no accessibility problems. So explaining why it's not working in a particular browser is probably not required in most cases.

                  Comment

                  • Rsmastermind
                    New Member
                    • Sep 2008
                    • 93

                    #10
                    Yes acoder that is fine but if that is requirement then we sholuld explain why the thing is malfunctioning. Thats what I had told there might be any other case also where people need not the copy/paste option but they want to disable the right click as per the code pasted by Archana.

                    Archana's code as she explained is running in IE but not in Mozila.So I had told to focus on this.Nothing else then that

                    What you suggested that is much fine.But my attention was to focus the problem as she mentioned.Pleas e dont mind this otherwise.

                    Comment

                    • Archanak
                      New Member
                      • Sep 2006
                      • 79

                      #11
                      Originally posted by Rsmastermind
                      Both of my senior friends are correct but that is what the buisness concern.If someone wants to disable the right click not for the text field but for the pictures then the logic what my seniors has given is not correct.

                      This is about the technical concept what we are dealing .Right now for Archanak she wants to do it for the text field and she is getting some problem wth the mozila explorer so instead of talking about the business we should tell her about why the problem is.Isn't it my friends?

                      So, technically if you want to disable the right click of the mouse for the whole document then oncontextmenu is helpful.
                      But for the different tags you can't use this .So what you had done is ok with IE.

                      For mozila Add this piece of code where e is the event.As Mozila does not behave same as IE in context of script handling.
                      Code:
                      function ffdisable(e)
                      {
                      if (e.button==2)
                        { //firefox disable
                        e.preventDefault();
                        e.stopPropagation();
                        alert(message);
                        return false;
                        }
                      }
                      Hi ,

                      Thanks for your reply!!!!
                      I tried but its giving that message("No right click is allowed) and along with that (cut,copy,paste ,selectall) option menu is also displayed.

                      There is no solution for this problem?
                      with regards
                      Archana

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        There is, but you're going about it in the wrong way. Have you tried the code in the thread I linked to earlier?

                        Comment

                        • Archanak
                          New Member
                          • Sep 2006
                          • 79

                          #13
                          Originally posted by acoder
                          There is, but you're going about it in the wrong way. Have you tried the code in the thread I linked to earlier?
                          Hi,

                          I tried!!!

                          But i am getting context menu(cut,copy,p aste)

                          I don't want context menu to come!!!

                          I have only one text box to take email id.

                          How should i call this function?

                          with regards
                          Archana

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            The code won't prevent the context menu from appearing because it doesn't need to. It detects a paste and lets the user know that the second email has to be typed in. Surely, that's what you wanted? It has the advantage of keeping all the browser functionality and still meeting your requirements.

                            Comment

                            • Archanak
                              New Member
                              • Sep 2006
                              • 79

                              #15
                              Originally posted by acoder
                              The code won't prevent the context menu from appearing because it doesn't need to. It detects a paste and lets the user know that the second email has to be typed in. Surely, that's what you wanted? It has the advantage of keeping all the browser functionality and still meeting your requirements.
                              Hi,

                              Is it possible to do such that context menu should not appear or even paste option should not be highlighted?

                              It works fine in IE i.e context menu will not appear in IE. but context menu appears in firefox.

                              How to avoid that??

                              With regards
                              Archana

                              Comment

                              Working...