JQuery prevent dragging text value

I have an html textbox that I have bound a function via jQuery to an insert event to prevent users from pasting a value into the textbox. This functionality works well.

However, you can select text from another text box on the page and drag it to the text box where pastes are prevented. Is there a jQuery event I can bind to this that will prevent users from dragging and dropping text in the texbox?

+2


a source to share


3 answers


First of all, you shouldn't prevent the user from copying and pasting, or dragging and dropping from one input to another. Most people despise the email confirmation input field and I am one of them. Making it harder to fill out will only annoy your users.

However ... a break-in warning ...

You cannot block built-in browser functionality, however you can disable text selection on inputs you do not want to drag, as shown below:

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 that would be REALLY annoying.



OPTION 2:

You can turn off the confirmation box when the user drags the element:

$(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 favor for the user and get rid of the confirmation box.

+3


a source


Two solutions

1 You can frame the problem this way: you want the user to use the keyboard to enter a value. You can count the number of keystrokes. Then, in a change event, when the input loses focus, you must have a number of keystrokes at least equal to the number of characters in the value.



2 You can use the spacing function to frequently check for differences in the meaning of an input. If the difference is too many characters, undo this change, because it can only be related to text nesting. It is up to you to decide the frequency and maximum number of keystrokes that are humanly achievable during this period of time.

0


a source


Unfortunately this is not possible.

-1


a source







All Articles