[javascript] 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.

This question is related to javascript html copy-paste

The answer is


Add a second step to your registration process. First page as usual, but on reload, display a second page and ask the email again. If it's that important, the user can handle it.


Using jquery, you can avoid copy paste and cut using this

$('.textboxClass').on('copy paste cut', function(e) {
    e.preventDefault();
});

Crazy idea: Require the user to send you an email as part of the signup process. This would obviously be inconvenient when clicking on a mailto link doesn't work (if they're using webmail, for example), but I see it as a way to simultaneously guarantee against typos and confirm the email address.

It would go like this: They fill out most of the form, entering their name, password, and whatnot. When they push submit, they're actually clicking a link to send mail to your server. You've already saved their other information, so the message just includes a token saying which account this is for.


I did something similar to this for http://bookmarkchamp.com - there I wanted to detect when a user copied something into an HTML field. The implementation I came up with was to check the field constantly to see if at any time there was suddenly a whole lot of text in there.

In other words: if once milisecond ago there was no text, and now there are more than 5 chars... then the user probably pasted something in the field.

If you want to see this working in Bookmarkchamp (you need to be registered), paste a URL into the URL field (or drag and drop a URL in there).


You can..... but don't.

You should not be altering the default behaviour of a users browser. It really is bad usability for your web application. Also if a user wants to disable this hack then they can just disable javascript on their browser.

Just add these attributes to the textbox

ondragstart=”return false” onselectstart=”return false”

You can disable the copy paste option with jQuery by the below script:

jQuery("input").attr("onpaste","return false;");

of by using the bellow oppaste attribute into the input fields.

onpaste="return false;"

I use this vanilla JS solution:

const element = document.getElementById('textfield')
element.addEventListener('paste', e =>  e.preventDefault())

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();
    });
  });

Hope below code will work :

<!--Disable Copy And Paste-->
<script language='JavaScript1.2'>
function disableselect(e){
return false
}
function reEnable(){
return true
}
document.onselectstart=new Function ("return false")
if (window.sidebar){
document.onmousedown=disableselect
document.onclick=reEnable
}
</script>

what about using CSS on UIWebView? something like

<style type="text/css">
<!—-
    * {
        -webkit-user-select: none;
    }
-->
</style>

also you can read detail about block copy-paste using CSS http://rakaz.nl/2009/09/iphone-webapps-101-getting-safari-out-of-the-way.html


if you have to use 2 email fields and are concerned about the user incorrectly pasting the same mistyped email from field 1 to field 2 then i'd say show an alert (or something more subtle) if the user pastes something into the second email field

document.querySelector('input.email-confirm').onpaste = function(e) {
    alert('Are you sure the email you\'ve entered is correct?');
}

this way you don't disable paste, you just give them a friendly reminder to check what they've presumably typed in the first field and then pasted to the second field is correct.

however, perhaps a single email field with autocomplete on is all that's needed. chances are they've filled their email in correctly before on another site at some point and the browser will suggest to fill the field with that email

<input type="email" name="email" required autocomplete="email">

Extending @boycs answer, I would recommend also using "on".

$('body').on('copy paste', 'input', function (e) { e.preventDefault(); });

Just got this, we can achieve it using onpaste:"return false", thanks to: http://sumtips.com/2011/11/prevent-copy-cut-paste-text-field.html

We have various other options available as listed below.

<input type="text" onselectstart="return false" onpaste="return false;" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" autocomplete=off/><br>

The way that I would resolve the issue of confirming an email address is as follows:

  1. Before going through the main process - say registering the user - first ask them to enter their email address.
  2. Generate a unique code and send it to that email address.
  3. If user has entered the correct email address, they will get the code.
  4. User must enter that code along with their email address, and their other required information, so they can complete the registration. - Please note that if this time they enter a wrong email address (or a wrong code), because it will not match with the code, the registration will not go through, and the user will be informed right away.
  5. If the email address, the code, and other registration information have been entered correctly, the registration is complete and user can start using the system immediately. - no need to to respond to any other email address in order to activate their account

For better security, the code should have a limited lifetime, and it should be allowed only once in the registration process. Also, in order to prevent any malicious robot applications, it is better to accompany the first step with captcha or a similar mechanism.


With Jquery you can do this with one simple codeline.

HTML:

<input id="email" name="email">

Code:

$(email).on('paste', false);

JSfiddle: https://jsfiddle.net/ZjR9P/2/


Check validity of the MX record of the host of the given email. This can eliminate errors to the right of the @ sign.

You could do this with an AJAX call before submit and/or server side after the form is submitted.


Simple solution: just reverse the registration process: instead of requiring confirmation at the end of registration process, request confirmation at the beginning of it! I.e. the registration process started with a simple form asking for e-mail address and nothing else. Upon submitting, an e-mail with link to a confirmation page unique to the e-mail address sent out. The user go to that page, then the rest of information for the registration (user name, full name, etc.) will be requested.

This is simple since the website does not even need to store anything before confirmation, the e-mail address can be encrypted with a key and attached as part of the confirmation page address.


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).


from

Some may suggest using Javascript to capture the users' actions, like right-clicking the mouse or the Ctrl+C / Ctrl+V key combinations and then stopping the operation. But this is obviously not the best or simplest solution. The solution is integrated in the input field properties itself together with some event capturing using Javascript.

In order to disabled the browsers' autocomplete, simply add the attribute to the input field. It should look something like this:

<input type="text" autocomplete="off">

And if you want to deny Copy and Paste for that field, simply add the Javascript event capturing calls oncopy, onpaste, and oncut and make them return false, like so:

<input type="text" oncopy="return false;" onpaste="return false;" oncut="return false;">

The next step is using onselectstart to deny the input field's content selection from the user, but be warned: this only works for Internet Explorer. The rest of the above work great on all the major browsers: Internet Explorer, Mozilla Firefox, Apple Safari (on Windows OS, at least) and Google Chrome.


You can use jquery

HTML file

<input id="email" name="email">

jquery code

$('#email').bind('copy paste', function (e) {
        e.preventDefault();
    });

You could attach a "keydown" listener to the input box to detect whether or not the Ctrl + V keys are being pressed and, if so, stop the event or set the input box's value to ''.

That wouldn't handle right clicking and pasting or pasting from the Edit menu of the browser, though. You may need to add a "last length" counter to the keydown listener and use an interval to check the field's current length to see if it increase since the last keystroke.

Neither is recommended, though. Form fields with paste disabled are extremely frustrating. I'm capable of typing my email correctly the first time, so I reserve the right to paste it into the second box.


How about sending a confirmation email to the email address that the user has just entered twice in which there is a link to a confirmation URL on your site, then you know that they have got the message?

Anyone that doesn't click to confirm the receipt of the email may have entered their email address incorrectly.

Not a perfect solution, but just some ideas.


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to copy-paste

Copy Paste Values only( xlPasteValues ) HTML page disable copy/paste VBA copy rows that meet criteria to another sheet How to Copy Text to Clip Board in Android? Copy/Paste/Calculate Visible Cells from One Column of a Filtered Table vi/vim editor, copy a block (not usual action) New Line Issue when copying data from SQL Server 2012 to Excel jQuery bind to Paste Event, how to get the content of the paste How to paste text to end of every line? Sublime 2 copy all files and folders from one drive to another drive using DOS (command prompt)