+ Start a Discussion
krishna casukhela 7krishna casukhela 7 

hide email with askterisk if present

Hello friends
I have a requirement as follows. I am explaining in detail.

I have a URL say https://ap2.salesforce.com?fanID=0032800000gufvJ  in salesforce.
based on this FanID I will get the email of that user.

Now I have a visualforce page with standard controller="fan__c"
<apex:inputText name="email>

In aex controller:
string fanid="Apexpages.currentpage().getparameter('fanID');
               if fanid != NULL
  string emaladdress=[select Email from fan__c where ID=:fanID];

Now value in emailaddress should be stored in <apex:InputText name=Email>

if the email already exists it should be replaced with asterisk as follows.
for example : assume email is  jose@gmail.com and if it exists for that particular ID then the contents of <apex:inputText> should be
j***@g****.com

If email doesnt exist for that  FanID   then contents of <apex:inputText> will contain   mary@yahoo.co.in

I am literaly struggling with this so any will be rewarded with points

thanks
krishna
Best Answer chosen by krishna casukhela 7
LBKLBK
Hi Krishna,

a)  if email and mobile phone exists then they should be masked. so this part is fine.
     if mobilephone doesnt exists then user enters a new mobile phone number then while saving to sobject the it should be masked.
  LBK: I don't understand this part. When the user enters Mobile Phone / Email address, you don't have to mask it until the page is refreshed (Because it is he / she who entered the values anyways).
When the page is refreshed, masking will be taken care already.


b) cud u pls let mw know the SAVE functionality, I need to save to sobject contact the values. adding command button
LBK: I have added Save functionality in the code. After save, the page will refresh itself. Make sure you change "THIS_VF_PAGE" with the actual VF page name in the code.
VF Page
<apex:page controller="clsContact" >
    <apex:form >
        
        <apex:inputField value="{!contact.FirstName}"/>
        <apex:inputText value="{!maskedEmail}"/>
        <apex:inputText value="{!maskedMobilePhone}"/>
		<apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>


APEX Controller
public class clsContact {
    public Contact contact {get; set;}
    public String maskedEmail{get; set;}
    public String maskedMobilePhone{get; set;}
    String contactId {get; set;} 
    
    public clsContact(){
        contactId = ApexPages.currentPage().getParameters().get('id');
        if(contactId != null){
            getContact();
        }
        else{
            contact = new Contact();
        }
    }
    public void getContact() {
        
            contact = [SELECT Id, FirstName,Email,MobilePhone FROM Contact WHERE Id = :contactId];
            maskedEmail = maskEmail(contact.Email);
            maskedMobilePhone = maskPhone(contact.MobilePhone);
    }
    
    private String maskEmail (String sEmail){
         String sMaskedEmail;
         String[] aEmail = sEmail.split('@');
         if(aEmail.size() == 2){
             sMaskedEmail = aEmail[0].left(1);
             for(integer i=0; i < aEmail[0].length() - 1; i++){
                 sMaskedEmail += '*';
             }
             
             String[] aEmail2 = aEmail[1].split('\\.', 2);
             sMaskedEmail += '@' + aEmail2[0].left(1);
             for(integer i=0; i < aEmail2[0].length() - 1; i++){
                 sMaskedEmail += '*';
             }
             sMaskedEmail += '.' + aEmail2[1];
         }
         return sMaskedEmail;   
    }

	private String maskPhone(String sPhone)
	{
		String sMaskedMobilePhone="*";	  
		for(integer i=0;i<=sPhone.lenght();i++)
		{
			sMaskedMobilePhone+="*";
		}
		return sMaskedMobilePhone;
	}

	public PageReference save (){
		contact.Email = maskedEmail;
		contact.MobilePhone = maskedMobilePhone;
		upsert contact;

		PageReference self = new PageReference('/apex/THIS_VF_PAGE?id=' + (String)contact.id);
		self.setRedirect(true);
		return self;
	}
}
Let me know how it goes.
 

All Answers

Malni Chandrasekaran 2Malni Chandrasekaran 2
Krishna,
Please try this:

if (<condition to check if already exists>)
{

string eID = 'abcdefgh@gmail.com';  // Replace this eID with your string variable emailAddress.

integer len = eID.length();
string maskedId = eID.substring(0,1); // Masked Id will be your resultant Id 
System.debug('0 ' +maskedId);
integer i;
for (i = 1; i< (len-4); i++)
{if (eID.substring(i,i+1) == '@')
    {maskedId = maskedId + eID.substring(i, i+2);  
        i = i+1;
            System.debug(i + ' ' + maskedId);
    }
    else{
    maskedId = maskedId + '*'; 
        System.debug(i + ' ' +maskedId);
    }
}
maskedId = maskedId + eId.substring(i, eId.length());  //Put this masked Id in input text field
System.debug(maskedId);
}
else
{
 //put unmasked email Id in the input field.
}



Please mark it as a answer if it solves your problem to benefit other viewers.
Karan Shekhar KaulKaran Shekhar Kaul
Hi Krishna,

Below code should give you idea about how to implement this.

<apex:page standardController="fan__c" id="pg" >
    <apex:form id="frm">
        <apex:inputText id="actualEmail" />
        <apex:inputHidden value="{!fan__c.email}" id="email"/>
    </apex:form>
    
    <script>
    
    window.onload = function(){email()} 
    
    function email(){
        var email = document.getElementById('pg:frm:email').value;
        if(email != null){
            var maskedEmail = maskEmail(email);
            if(maskedEmail!= null && maskedEmail!=''){
                
                document.getElementById('pg:frm:actualEmail').value = maskedEmail;
            }
        }
    }
    
    function maskEmail(emailToMask){
        
        var maskid = "";
        var prefix= emailToMask.substring(0, emailToMask.lastIndexOf("@"));
        var postfix= emailToMask.substring(emailToMask.lastIndexOf("@"));
        
        for(var i=0; i<prefix.length; i++){
            if(i == 0 || i == prefix.length - 1) {   ////////
                maskid = maskid + prefix[i].toString();
            }
            else {
                maskid = maskid + "*";
            }
        }
        maskid =maskid +postfix;
        
        console.log(maskid);
        return maskid;
        
    }
    </script>
</apex:page>
LBKLBK
Hi,

Here is the VF and APEX extract from the sample implementation on Contact.

Let me know if this helps.
 
VF Page
<apex:page controller="clsContact" >
    <apex:form >
        <apex:inputField value="{!contact.FirstName}"/>
        <apex:inputField value="{!contact.LastName}"/>
        <apex:inputField value="{!contact.Email}"/>  
        <apex:inputText value="{!maskedEmail}"/>
    </apex:form>
</apex:page>


APEX Controller
public class clsContact {
    public Contact contact {get; set;}
    public String maskedEmail{get; set;}
    String contactId {get; set;} 
    
    public clsContact(){
        contactId = ApexPages.currentPage().getParameters().get('id');
        if(contactId != null){
            getContact();
        }
        else{
            contact = new Contact();
        }
    }
    public void getContact() {
        
            contact = [SELECT Id, FirstName, LastName, Email FROM Contact WHERE Id = :contactId];
            maskedEmail = maskEmail(contact.Email);
    }
    
    private String maskEmail (String sEmail){
         String sMaskedEmail;
         String[] aEmail = sEmail.split('@');
         if(aEmail.size() == 2){
             sMaskedEmail = aEmail[0].left(1);
             for(integer i=0; i < aEmail[0].length() - 1; i++){
                 sMaskedEmail += '*';
             }
             
             String[] aEmail2 = aEmail[1].split('\\.', 2);
             sMaskedEmail += '@' + aEmail2[0].left(1);
             for(integer i=0; i < aEmail2[0].length() - 1; i++){
                 sMaskedEmail += '*';
             }
             sMaskedEmail += '.' + aEmail2[1];
         }
         return sMaskedEmail;   
    }

}

 
krishna casukhela 7krishna casukhela 7

Hi LBK
Ur code works as per my expectation.
VF Page
<apex:page controller="clsContact" >
    <apex:form >
        
        <apex:inputField value="{!contact.FirstName}"/>
        <apex:inputText value="{!maskedEmail}"/>
        <apex:inputText value="{!maskedMobilePhone}"/>

    </apex:form>
</apex:page>


APEX Controller
public class clsContact {
    public Contact contact {get; set;}
    public String maskedEmail{get; set;}
    public String maskedMobilePhone{get; set;}
    String contactId {get; set;} 
    
    public clsContact(){
        contactId = ApexPages.currentPage().getParameters().get('id');
        if(contactId != null){
            getContact();
        }
        else{
            contact = new Contact();
        }
    }
    public void getContact() {
        
            contact = [SELECT Id, FirstName,Email,MobilePhone FROM Contact WHERE Id = :contactId];
            maskedEmail = maskEmail(contact.Email);
            maskedMobilePhone = maskPhone(contact.MobilePhone);
    }
    
    private String maskEmail (String sEmail){
         String sMaskedEmail;
         String[] aEmail = sEmail.split('@');
         if(aEmail.size() == 2){
             sMaskedEmail = aEmail[0].left(1);
             for(integer i=0; i < aEmail[0].length() - 1; i++){
                 sMaskedEmail += '*';
             }
             
             String[] aEmail2 = aEmail[1].split('\\.', 2);
             sMaskedEmail += '@' + aEmail2[0].left(1);
             for(integer i=0; i < aEmail2[0].length() - 1; i++){
                 sMaskedEmail += '*';
             }
             sMaskedEmail += '.' + aEmail2[1];
         }
         return sMaskedEmail;   
    }

private String maskPhone(String sPhone)
{
  String sMaskedMobilePhone="*";
  
  for(integer i=0;i<=sPhone.lenght();i++)
  {
   
    sMaskedMobilePhone+="*";
  }

}

return sMaskedMobilePhone;
}

Please help me out in understanding two things.
a)  if email and mobile phone exists then they should be masked. so this part is fine.
     if mobilephone doesnt exists then user enters a new mobile phone number then while saving to sobject the it should be masked.

b) cud u pls let mw know the SAVE functionality, I need to save to sobject contact the values. adding command button

thanks
Krishna
LBKLBK
Hi Krishna,

a)  if email and mobile phone exists then they should be masked. so this part is fine.
     if mobilephone doesnt exists then user enters a new mobile phone number then while saving to sobject the it should be masked.
  LBK: I don't understand this part. When the user enters Mobile Phone / Email address, you don't have to mask it until the page is refreshed (Because it is he / she who entered the values anyways).
When the page is refreshed, masking will be taken care already.


b) cud u pls let mw know the SAVE functionality, I need to save to sobject contact the values. adding command button
LBK: I have added Save functionality in the code. After save, the page will refresh itself. Make sure you change "THIS_VF_PAGE" with the actual VF page name in the code.
VF Page
<apex:page controller="clsContact" >
    <apex:form >
        
        <apex:inputField value="{!contact.FirstName}"/>
        <apex:inputText value="{!maskedEmail}"/>
        <apex:inputText value="{!maskedMobilePhone}"/>
		<apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>


APEX Controller
public class clsContact {
    public Contact contact {get; set;}
    public String maskedEmail{get; set;}
    public String maskedMobilePhone{get; set;}
    String contactId {get; set;} 
    
    public clsContact(){
        contactId = ApexPages.currentPage().getParameters().get('id');
        if(contactId != null){
            getContact();
        }
        else{
            contact = new Contact();
        }
    }
    public void getContact() {
        
            contact = [SELECT Id, FirstName,Email,MobilePhone FROM Contact WHERE Id = :contactId];
            maskedEmail = maskEmail(contact.Email);
            maskedMobilePhone = maskPhone(contact.MobilePhone);
    }
    
    private String maskEmail (String sEmail){
         String sMaskedEmail;
         String[] aEmail = sEmail.split('@');
         if(aEmail.size() == 2){
             sMaskedEmail = aEmail[0].left(1);
             for(integer i=0; i < aEmail[0].length() - 1; i++){
                 sMaskedEmail += '*';
             }
             
             String[] aEmail2 = aEmail[1].split('\\.', 2);
             sMaskedEmail += '@' + aEmail2[0].left(1);
             for(integer i=0; i < aEmail2[0].length() - 1; i++){
                 sMaskedEmail += '*';
             }
             sMaskedEmail += '.' + aEmail2[1];
         }
         return sMaskedEmail;   
    }

	private String maskPhone(String sPhone)
	{
		String sMaskedMobilePhone="*";	  
		for(integer i=0;i<=sPhone.lenght();i++)
		{
			sMaskedMobilePhone+="*";
		}
		return sMaskedMobilePhone;
	}

	public PageReference save (){
		contact.Email = maskedEmail;
		contact.MobilePhone = maskedMobilePhone;
		upsert contact;

		PageReference self = new PageReference('/apex/THIS_VF_PAGE?id=' + (String)contact.id);
		self.setRedirect(true);
		return self;
	}
}
Let me know how it goes.
 
This was selected as the best answer