NibblyPig
NibblyPig

Reputation: 52952

Possible to stop copy + pasting into a html textbox?

Got a password field, and ideally we want people to type their password rather than copy and paste it in. Is there an easy way using Javascript?

Upvotes: 0

Views: 1682

Answers (5)

RobKohr
RobKohr

Reputation: 6953

$(':password').bind('paste', function(e) {
    return false;
})

Upvotes: 1

sushil bharwani
sushil bharwani

Reputation: 30197

You can do this with the help of Polling Technique. Hoewver this is not the suggested way for textboxes. If you have observed some of the Bank site allows you to enter password via a keypad (with mouse clicks). And password field is disabled for keyboard entry

Upvotes: 0

Victor Nicollet
Victor Nicollet

Reputation: 24587

First things first: this a bad idea.

You could use the key press events on the password field to detect what key was pressed, to check whether the change in the password field matches the event (if they press "a", then check if the letter "a" was indeed inserted), and restore the previous value (with an error message) if it does not.

Of course, people will just have their web browser remember their password for them.

Some banks replace the password field with an image of digits where you have to click to enter the password (here, for instance). You could use JavaScript to replace the password field with that image when JavaScript is enabled.

Upvotes: 1

Andy E
Andy E

Reputation: 344733

Not consistently across all browsers. Most browsers (not Opera, Firefox 2) support the cancellable onpaste event:

document.getElementById("password").onpaste = function () { 
    return false;
}

https://developer.mozilla.org/en/DOM/element.onpaste

Upvotes: 5

Martin Hennings
Martin Hennings

Reputation: 16866

You can measure the time it takes from the first to the last change. If it was too short, clear the field...

Upvotes: 0

Related Questions