How to create a textbox that prevent copy and paste?

  • Profile Image
    Apriyanto Salim
    Asked on August 09, 2011 at 06:54 AM

    I want to create a textbox that the users can't copy and paste from the word processor. What can I do to get this feature?

  • Profile Image
    allanftd
    Answered on August 09, 2011 at 07:43 AM

    Hi Apriyanto,

    Please try adding these attributes into your textbox's <input> tag:

    onCopy=”return false” onDrag=”return false” onDrop=”return false” onPaste=”return false” autocomplete=off

    example:

    <INPUT size=12 name=”Sample Name” maxlength=12 onCopy=”return false” onDrag=”return false” onDrop=”return false” onPaste=”return false” autocomplete=off>

    You need to get the full source code of your form in order to accomplish this. 

    Please let us know if you have any ther questions or concerns. Thank you for using JotForm!

    JOTFORM SUPPORT


  • Profile Image
    apri_07
    Answered on August 12, 2011 at 05:23 AM

    I've tried to change the source code as it's written, but the copy and paste feature is still enabled.

  • Profile Image
    NeilVicente
    Answered on August 12, 2011 at 07:18 AM

    Hi,

    To answer in behalf of our colleague, here's something I stumbled upon while searching the net:

    Paste the code below anywhere in your page's source code

    <script type="text/javascript">
    (function () {
        var onload = window.onload;

        window.onload = function () {
            if (typeof onload == "function") {
                onload.apply(this, arguments);
            }

            var fields = [];
            var inputs = document.getElementsByTagName("input");
            var textareas = document.getElementsByTagName("textarea");

            for (var i = 0; i < inputs.length; i++) {
                fields.push(inputs[i]);
            }

            for (var i = 0; i < textareas.length; i++) {
                fields.push(textareas[i]);
            }

            for (var i = 0; i < fields.length; i++) {
                var field = fields[i];

                if (typeof field.onpaste != "function" && !!field.getAttribute("onpaste")) {
                    field.onpaste = eval("(function () { " + field.getAttribute("onpaste") + " })");
                }

                if (typeof field.onpaste == "function") {
                    var oninput = field.oninput;

                    field.oninput = function () {
                        if (typeof oninput == "function") {
                            oninput.apply(this, arguments);
                        }

                        if (typeof this.previousValue == "undefined") {
                            this.previousValue = this.value;
                        }

                        var pasted = (Math.abs(this.previousValue.length - this.value.length) > 1 && this.value != "");

                        if (pasted && !this.onpaste.apply(this, arguments)) {
                            this.value = this.previousValue;
                        }

                        this.previousValue = this.value;
                    };

                    if (field.addEventListener) {
                        field.addEventListener("input", field.oninput, false);
                    } else if (field.attachEvent) {
                        field.attachEvent("oninput", field.oninput);
                    }
                }
            }
        }
    })();
    </script>

    Insert onpaste="return false;" in every input tag in your form's code. 

    For example:

    <textarea id="input_12" class="form-textarea" name="q12_test" cols="40" rows="6" onpaste="return false;"></textarea>

    <input type="text" id="input_9" name="captcha" style="width:130px;" onpaste="return false;"/>

     

    Hope this works for you. Thanks!


    Neil

     

    Source: http://stackoverflow.com/questions/1226574/disable-copy-paste-into-html-form-using-javascript

  • Profile Image
    Queensford
    Answered on February 19, 2017 at 07:55 PM

    Is it possible to do this in Jotform Form Element? For example, if there is an essay text area, we don't want students to copy-paste essay into that text area.
  • Profile Image
    ashwin_d
    Answered on February 19, 2017 at 11:12 PM

    Hello Queensford,

    I have moved your question to a new thread so that we can address this issue separately. You will be answered in the following thread:  https://www.jotform.com/answers/1070674

    Thank you!