2

I have a html textbox on which I've bound a function via jQuery to the paste event to prevent users pasting a value into the textbox. This functionality works well.

However it is possible to select some text from another textbox on the page and drag it into the textbox in which pastes are prevented. Is there a jQuery event that I can bind to that will prevent users dragging text into the texbox?

ipr101
  • 24,096
  • 8
  • 59
  • 61
  • Why do you care if the user types something or copies it into the input? Is it a typing test? – tvanfosson May 06 '10 at 12:17
  • It's a confirm email textbox. – ipr101 May 06 '10 at 12:27
  • 5
    If someone wants to copy+paste between "e-mail" and "confirm e-mail", my advice would be to just let them. They're already pissed off that you're insulting them by asking them to enter their address twice. They'll be even more annoyed if you've taken special measures to circumvent their laziness. – Iain Galloway May 06 '10 at 12:54
  • 4
    don't do it man. For the love of God...don't do it! – David Murdoch May 06 '10 at 13:06

5 Answers5

3

First of all you shouldn't prevent the user from being able to copy and paste or click and drag from one input to another. Most people despise the confirm email input box, and I'm one of them. Making it harder to fill it in will only serve to irritate your users.

However... warning hack alert...

You cannot block the built in browser functionality, however you could disable the text from being selected in the inputs you don't want to be dragged like follows:

OPTION 1:

$(document).ready(function () {

    // Get the control from which the drag should be disallowed
    var originator = $(".start");

    // Disable text selection
    if ($.browser.mozilla) {
        originator.each(function () { $(this).css({ 'MozUserSelect': 'none' }); });
    } 
    else if ($.browser.msie) {
        originator.each(function () { $(this).bind('selectstart.disableTextSelect', 
           function () { return false; }); });
    } 
    else {
        originator.each(function () { $(this).bind('mousedown.disableTextSelect', 
           function () { return false; }); });
    }

});

But, this would be REALLY annoying.

OPTION 2:

You could disable the confirmation box when the user is dragging an item:

$(document).ready(function () {

    // Get the control which dropping should be disallowed
    var originator = $("#emailBox");

    // Trap when the mouse is up/down
    originator.mousedown(function (e) {
        $("#confirmationBox").attr('disabled', 'disabled');
    });
    originator.mouseup(function (e) {
        $("#confirmationBox").attr('disabled', '');
    });

});

OPTION 3:

Do your user-base a favour and get rid of the confirmation box.

djdd87
  • 67,346
  • 27
  • 156
  • 195
  • You have no _idea_ how annoying this would be. – SLaks May 06 '10 at 12:39
  • @SLaks I certainly know exactly how annoying it would be. I've rewritten the answer especially for you. – djdd87 May 06 '10 at 13:05
  • 1
    can you post option 3 as a separate answer? I'll +1 to that! – David Murdoch May 06 '10 at 13:09
  • 1
    Boo. Yesterday someone posted "The one fix for all IE6 problems is to stop supporting IE6." as an answer to [this question](http://stackoverflow.com/questions/2773464/one-fix-for-all-ie6-problems) and has since been up-voted 35 times. – David Murdoch May 06 '10 at 13:21
2

In my case, based on @Prateek's answer I did something in jquery: I've added to some specific text inputs the following attribute: ondrop="return false;"...

My code was:

// Prevents to drag and drop from text input fields.
    $("#MyInput").attr("ondrop","return false;");

After adding this, it is working perfectly.

Hope this can help others.

KAZZABE
  • 197
  • 2
  • 10
1

Add ondrop="return false;" to those input elements where you want to disable dropping text.

input {
  -webkit-user-select: auto;
  user-select: auto;
  -webkit-user-drag: none;
}
<article>
  <input type="text" placeholder="Write text, select it, and drag" >
  <input type="text" placeholder="Then drop text here" ondrop="return false;">
</article>
Prateek
  • 1,229
  • 17
  • 31
0

Two solutions

1 You can phrase the issue in this way: You want the user to use the keyboard for inputting the value. You could count the number of keystrokes. Then, on the change event, when the input looses focus you should have the number of keystrokes at least equal to the number of the characters in the value.

2 You can use a interval function to frequently check differences in the value of the input. If a difference consists in too many characters all of a sudden, dismiss the change because it can only be due to text pasting. It is up to you to decide on a frequency and on a maximum number of keystrokes considered humanly achievable in that time frame.

Radu Simionescu
  • 4,518
  • 1
  • 35
  • 34
-3

Unfortunately, this is not possible.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • sure it is...but I'm not going to provide the solution...not even for a million rep bounty! – David Murdoch May 06 '10 at 13:07
  • Does it involve breaking into Redmond and 'tinkering' with some Source? – djdd87 May 06 '10 at 13:11
  • Yes. Unfortunately there is one major drawback: besides copy/paste functionality being disabled on inputs --- that "teenie, tiny" modification may be all it takes to turn IE into skynet...could you image the devastation and the horror of IE taking over the world?!? – David Murdoch May 06 '10 at 13:17
  • I disagree. In cases like this, you can put up roadblocks to someone who wants to copy something. If the perceived reward for copying the text exceeds the perceived effort to do so, the user will copy the text anyways. In many cases, though, casual copying of text can very well be prevented. – Vivian River Mar 22 '12 at 20:13
  • This is actually the correct answer. The question is simple. All other answers are assuming the user wants to achieve some functionality and provide some workarounds. But in the end, disabling the dragging of a selected text (be it in an input field or not) is not possible afaik – Radu Simionescu Jul 25 '13 at 08:13