Monday 24 February 2014

Preventing Copy,Paste and Right Click In Your Page

Hi Guys
Everybody must have visited bank site there we can not do right click or copy Account number and paste in another field Retype Account Number. That means we should not give any chance to anybody to see page source. We do this for more security purpose. 
That made me to think and try to design same in visual-force page. Here I have only used JavaScript for this functionality. 
There are some snippet of code.Hopefully It will help you. 

Functionality 

  • Preventing to see the Page Source.
  • Preventing paste Email field value in Re Email field.
Here is the page:-
<apex:page StandardController="Contact">
  <script>
      function DisableRightClick(event){    
        if (event.button==2)
        {
            alert("Right Click is not Allowed");       
        }
      }
     function DisableCtrlKey(e){
        
        var code = (document.all) ? event.keyCode:e.which;
         if (parseInt(code)==17){
            alert("Please re-type your email address");           
        }   
    }
  </script>
  <apex:form >
      <apex:pageBlock title="Create Contact" onmousedown="DisableRightClick(event)">
          <apex:pageBlockButtons >
              <apex:commandButton value="Save" action="{!save}"/>
          </apex:pageBlockButtons>
          <apex:pageBlockSection columns="1">
              <apex:inputField value="{!Contact.Firstname}"/>
              <apex:inputField value="{!Contact.LastName}"/>
              <apex:inputField value="{!Contact.Email}"/>
              <apex:inputtext value="{!Contact.Email}" label="Re-Type Email Id"  onKeyDown="DisableCtrlKey(event)"/>              
              <apex:inputField value="{!Contact.Phone}"/>              
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

1 comment:

madhu.com said...

This is good post and it is really helpful