• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1015
  • Last Modified:

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

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
ssdesign
Asked:
ssdesign
1 Solution
 
krunal_shahCommented:
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
 
Gurvinder Pal SinghCommented:
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
 
Michel PlungjanIT ExpertCommented:
This works for some browsers
<textarea onpaste="return false" oncopy="return false">

Mobile safari??? Possibly
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ssdesignAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
Yes, please share
0
 
ssdesignAuthor Commented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now