?
Solved

How to disable copy paste in textarea while still allowing to type

Posted on 2010-01-11
8
Medium Priority
?
1,012 Views
Last Modified: 2012-05-08
Hi,
I want to create a webpage where I do not want the user to be able to copy/paste inside a textarea.

At the same time, I want them to be able to enter text.

How can I achieve this?

Most importantly, it has to work on mobile safari.
0
Comment
Question by:ssdesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 10

Expert Comment

by:krunal_shah
ID: 26281896
Hi,

you can do it as following,

    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);")


for more details please check,
http://forums.asp.net/t/1125227.aspx
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26282566
try this

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;
}
}

<textarea onKeyDown="return noCTRL(event)"></textarea>

See this link also
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_25017122.html
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26283077
This works for some browsers
<textarea onpaste="return false" oncopy="return false">

Mobile safari??? Possibly
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:ssdesign
ID: 26300408
Hi,
I found the solution outside of this thread.

Except for the last post, All other solutions were for PC based whereas my question was more related to Mobile Safari.

The last one also pointed to the clipboard javascript documentation instead of addressing my specific question.

Sorry guys, but none of this solved my problem.
Instead I found my solution.

So If the moderator wants, I can share my solution and show that its quiet different from all these suggestions.

With all due respect to the people who tried to answer, can I close this question without awarding it to anyone?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26300561
Yes, please share
0
 
LVL 7

Accepted Solution

by:
ssdesign earned 0 total points
ID: 26301278
Found a Very easy solution.

You can set this in CSS:
 
* {
   -webkit-user-select: none;  /* prevent copy paste for all elements */
}

Or

.my_text_element {
   -webkit-user-select: text;  /* enable copy paste for elements with this class */
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question