开发者

Possible to stop copy + pasting into a html textbox?

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


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


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.


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜