Home > ASP.Net, Javascript > Disable and Restrict Copy Cut and Paste from on a Textbox

Disable and Restrict Copy Cut and Paste from on a Textbox


Hi

try this example to Disable and Restrict Copy Cut and Paste from on a textbox using Javascript

You will need two javascript functions for this:

 

   function noCopyMouse(e) {
        var isRight = (e.button) ? (e.button == 2) : (e.which == 3);
       
        if(isRight) {
            alert('You are prompted to type this twice for a reason!');
            return false;
        }
        return true;
    }

    function noCopyKey(e) {
        var forbiddenKeys = new Array('c','x','v');
        var keyCode = (e.keyCode) ? e.keyCode : e.which;
        var isCtrl;

        if(window.event)
            isCtrl = e.ctrlKey
        else
            isCtrl = (window.Event) ? ((e.modifiers & Event.CTRL_MASK) == Event.CTRL_MASK) : false;
   
        if(isCtrl) {
            for(i = 0; i < forbiddenKeys.length; i++) {
                if(forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) {
                    alert('You are prompted to type this twice for a reason!');
                    return false;
                }
            }
        }
        return true;
    } 

 
And a wee bit of code-behind to handle the two events for the textbox(es):

Textbox1.Attributes.Add("onmousedown", "return noCopyMouse(event);");
 Textbox1.Attributes.Add("onkeydown", "return noCopyKey(event);");

 Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. November 20, 2009 at 12:23 pm

    Hi onpaste=”event.returnValue=false;” will prevent cut,copy,paste in textbox….

  2. ali
    March 27, 2011 at 2:48 am

    can’t use it!

    need more explanation regarding how to use the ‘wee bit of code..’

    please explain more fully.

    thanks

  3. azar
    May 27, 2011 at 5:18 am

    working fine in IE but not working in firefox,can u plz fix in firefox also

  4. yasserzaid
    May 29, 2011 at 8:20 am

    @G.MuthuKumar , @Ali , @Azar :- you are welcome Good Luck

  5. vengatraman
    June 20, 2012 at 7:02 am

    me also met with same issue that is not working in firefox..plz help to do in firefox

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: