function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nisar AhmedNisar Ahmed 

Disable paste function for a inputText field in a VF page.

Hi All,

 

I have a inputText field in a VF page and I need to disable paste function(Ctrl+v and paste using mouse button) so that we need to compulsorily type in the text box instead of just copy pasting.

 

I have tried the following script:

<script>

function DisableCtrlKey(e)
{
    var code = (document.all) ? event.keyCode:e.which;
    // look for CTRL key press
    if (parseInt(code)==17)
    {
        alert("Please re-type your email address");
        window.event.returnValue = false;
    }
}

function DisableRightClick(event)
{
    //For mouse right click
    if (event.button==2)
    {
        alert("Please re-type your email address");       
    }
}

</script>

 and under the VF page:

<apex:inputText id="confirmemail" value="{!email}" onKeyDown="return DisableCtrlKey(event)" onMouseDown="DisableRightClick(event)" </apex:inputText>

 It works fine but I want to implement the same functionality without using alert function.

 

Can anyone please let me know any work around to accomplish the above scenario.

Any help on this will be highly appreciated.

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
tom_patrostom_patros

This is a bit vintage and uses jQuery, but it's a pretty nice reference for disabling copy/paste - http://www.4guysfromrolla.com/articles/060910-1.aspx

All Answers

tom_patrostom_patros

This is a bit vintage and uses jQuery, but it's a pretty nice reference for disabling copy/paste - http://www.4guysfromrolla.com/articles/060910-1.aspx

This was selected as the best answer
bob_buzzardbob_buzzard

This is more a browser question than a VF question.  The problem is that not all browsers support the events that are required, e.g, the following works for some but not all browsers:

 

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

 Most solutions I've seen involve 10s of lines of javascript to replicate the ondrop/onpaste events for browsers that don't support them.  I'd go along with Tom's suggestion and use a JQuery solution.

Nisar AhmedNisar Ahmed

Hi Tom and Bob,

 

That worked for me. Thanks.

 

Now another issue come up i.e., If we drag the text from a textbox to the confirm email textbox then the text is being copied.

I need to disable the drag and drop functionality for the textbox. I searched in the net nut nothing worked for me.

 

If possible could you/anyone please let me know how to disable drag and drop functionality for the textbox.

Thanks in Advance.

RajKumaR MRajKumaR M
Nisar,

I know it was very old post, however I also faced this situation, i tried with following script, it's working for me. It may help someone in future!!

function stopDefault(e) {
    if (e && e.preventDefault) {
        e.preventDefault();
    }
    else {
        window.event.returnValue = false;
    }
    return false;
}

Lynda Elbay 6Lynda Elbay 6
Hello, 

disable paste function and autocompletion directly on VisualForce : 
Here an exemple : 

 <apex:inputText value="{!emailConfirmation}" html-onpaste="return false;" html-autocomplete="off" html-placeholder="Confirmez votre e-mail" styleClass="inputs" html-type="Email" />

best regards 
Robert Wambold 10Robert Wambold 10

Lynda,

How do you put your code on say the the Case Console Page?

Kind regards,

Robert