Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable pasting text into HTML form

Is there a way using JavaScript to disable the ability to paste text into a text field on an HTML form?

E.g. I have a simple registration form where the user is required to input their email twice. The second email entry is to verify there are no typos in the first email entry. However if the user copy/pastes their email then that defeats the purpose and I've been experiencing users having problems because they've input the wrong email and copy/pasted it.

Maybe I wasn't clear on my question but I am not trying to prevent people from copying (or drag selecting) text on their browser. I just want to stop them from pasting input into a text field to minimize user error.

Perhaps instead of using this "hack" you can suggest another solution to the core problem of what I'm trying to solve here? I've done less than half a dozen user tests and this has already happened twice. My audience does not have a high level of computer proficiency.

like image 809
justinl Avatar asked Aug 04 '09 09:08

justinl


People also ask

How do you turn off paste in HTML?

The onpaste attribute lets us prevent pasting into the form. Adding the autocomplete attribute as well as preventing drag and drop into the element. If you want to avoid the on{event} code in the HTML, you can do it the cleaner way: myElement.

How do I restrict copy and paste TextBox in HTML?

The event. preventDefault() method stops the default action of an element from happening. You can use this method toprevent a TextBox control from Cut (Ctrl+X) , Copy (Ctrl+C) and Paste (Ctrl+V) .

How do I stop HTML from copying text from a website?

Here: How to disable text selection highlighting using CSS? -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none; Disallow them from being able to answer when the window's onBlur event is fired.

How do you paste text into input fields that block it?

Simply highlight the text and drag it into the text field. Try it here! This works for me in Firefox and Chrome. Great solution!


2 Answers

Don't do it. Don't mess with the user's browser. By Copy + Pasting into an E-Mail confirmation field, the user accepts responsibility over what they type. If they are dumb enough to copy + paste a faulty address (it has happened to me) then it's their own damn fault.

If you want to make sure that the E-Mail confirmation works out, have the user check their E-Mail while your site waits ("Please open your webmail program in a new window"). Show the E-Mail address in big fat letters ("The confirmation E-Mail was sent to.... made an error? CLick here to change).

Even better, if you can, let the user have some kind of limited access without confirming. That way, they can log in straight away and you improve your chances to keep in touch with the visitor even if the confirmation mail is blocked due to other reasons (e.g. spam filters).

like image 78
Pekka Avatar answered Sep 28 '22 01:09

Pekka


Add a class of 'disablecopypaste' to the inputs you want to disable the copy paste functionality on and add this jQuery script

  $(document).ready(function () {     $('input.disablecopypaste').bind('copy paste', function (e) {        e.preventDefault();     });   }); 
like image 20
Boycs Avatar answered Sep 28 '22 00:09

Boycs