locked
disable right click on a textBox WITHOUT alert RRS feed

  • Question

  • User-1738841732 posted

    I want below javascript to happen but without a alert, can someone help me

    function DisableRightClick(event)

     {

         if (event.button == 2)

          {

             alert("Right Clicking not allowed!");

             }

       }

    Wednesday, October 16, 2019 7:50 PM

Answers

  • User475983607 posted

    I want to disable right click on a text box, but i don't want alert(any popUp)

    Your code does not disable right click. It just handles a right click.   See the following SO post (or Google) for recommendations.

    https://stackoverflow.com/questions/737022/how-do-i-disable-right-click-on-my-web-page

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 9:27 PM
  • User288213138 posted

    Hi mansooraabid,

    According to your description, I made demo for your as a reference.

    I use e.preventDefault() in contextmenu event to disable  right click on a textbox.

    The code:

    <script type="text/javascript">
            $(document).ready(function () {
                $('#TextBox1').bind('contextmenu', function (e) {
                    e.preventDefault();
                });
            });
        </script>
    
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>         
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

    The result:

    More information about oncontextmenu you can refer to this link:https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/oncontextmenu

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 17, 2019 7:08 AM

All replies

  • User475983607 posted

    Rather than telling us what you do not want how about explaining what you do want?

    Wednesday, October 16, 2019 8:11 PM
  • User-1738841732 posted

    I want to disable right click on a text box, but i don't want alert(any popUp)

    Wednesday, October 16, 2019 8:23 PM
  • User475983607 posted

    I want to disable right click on a text box, but i don't want alert(any popUp)

    Your code does not disable right click. It just handles a right click.   See the following SO post (or Google) for recommendations.

    https://stackoverflow.com/questions/737022/how-do-i-disable-right-click-on-my-web-page

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 9:27 PM
  • User288213138 posted

    Hi mansooraabid,

    According to your description, I made demo for your as a reference.

    I use e.preventDefault() in contextmenu event to disable  right click on a textbox.

    The code:

    <script type="text/javascript">
            $(document).ready(function () {
                $('#TextBox1').bind('contextmenu', function (e) {
                    e.preventDefault();
                });
            });
        </script>
    
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>         
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

    The result:

    More information about oncontextmenu you can refer to this link:https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/oncontextmenu

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 17, 2019 7:08 AM