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
krishna casukhela 7krishna casukhela 7 

Unable to validate email in controller

I have a custom object called fan__c which has an email field, data type=Email.
when the page loads I append an ID to the URL so thereby I fetch the email vlaue from fan__c.
Also I am masking the mail value, when the user clicks SAVE button, the page refresh and it will show only the masked value to the user.

I need to check for a couple of things but unfortunately I am not getting this straight.
a) I should show an error message if the email field is blank when the user clicks SAVE button
b) assume the email field value is subbu123@gmail.com  and I change it to  subbu123    @gmail.com  OR I give as   subbu123@  and click SAVE button then it should throw me the default email validation error but its not happening.
any urgent help will be appreciated.

I have given the complete code
public class Pref6 
{
        
    public String fanEmail { get; set; } 
    public String fan_email{get;set;}  
    public fan__c fan{get;set;}
}

 public Pref6()
 {
        encryptedfanID=ApexPages.currentpage().getparameters().get('id');
               
        if(String.isBlank(encryptedfanID))
        {
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Id.');
                ApexPages.addMessage(myMsg);
                return;
        }
        else
        {
          try
          {
            fetchfanvalues();
          }
          catch(System.QueryException ex){
                
              Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, 'No Record'));
          }
   
        }}

  public void fetchfanvalues()
    {
         fan = new fan__c();
         
         fan=[SELECT id,Email__c,
              FROM fan__c 
              WHERE Encrypted_ID__c=:encryptedfanID];
         
         if (fan != NULL)
         {
             if(!string.isBlank(fan.Email__c))
             {
                fan_email=fan.Email__c;
                fanEmail=maskEmail(fan.Email__c);
             }
   }

//Here I mask the email address so when the user clicks SAVE button the page will refresh and he can see only the //masked values

public string maskEmail(string address) {        
        system.debug('Email Mask Called');
        string beforeEmailAddress = address.substringBefore('@');        
        string afterEmailAddress = address.substringAfter('@');        
                
        if(beforeEmailAddress.contains('.')){
            
            string emailNameBeforeDot = beforeEmailAddress.substringBefore('.');            
            string emailNameAfterDot = beforeEmailAddress.substringAfter('.');            
            
            emailAddress = emailNameBeforeDot.substring(0,1);
            
            for(integer i=0; i<emailNameBeforeDot.length()-1; i++){
                emailAddress += '*';                
            }
            
            emailAddress += '.';
            emailAddress += emailNameAfterDot.substring(0,1);
            
            for(integer i=0; i<emailNameAfterDot.length()-1; i++){
                emailAddress += '*';                
            }
            
            emailAddress += '@';            
            
        }else{
            
            emailAddress = beforeEmailAddress.substring(0,1);
            
            for(integer i=0; i<beforeEmailAddress.length()-1; i++){
                emailAddress += '*';                
            }
            
            emailAddress += +'@';
        }   
        
        if(afterEmailAddress.contains('.')) {
        
            string beforeEAddress = afterEmailAddress.substringBefore('.');
            string afterEAddress = afterEmailAddress.substringAfter('.');
            
            string eAddress = beforeEAddress.substring(0,1);
            
            for(integer i=0; i<beforeEAddress.length()-1; i++){
                
                eAddress += '*';
            }
            
            eAddress += '.' +afterEAddress;
            
            emailAddress += eAddress;        
        }
        return emailAddress;
    }
    
public void LoadMaskedValues()
   {
       fan = new fan__c();
       
       if (fan != NULL)
       {
      
           fan=[SELECT id, Email__c,Mobile_Phone__c
                       FROM fan__c 
                       WHERE Encrypted_ID__c=:encryptedfanID];
                       
           if(!string.isBlank(fan.Email__c))
           {
                fan_email=fan.Email__c;
                fanEmail=maskEmail(fan.Email__c);
           }
   }

public void SaveValues()
{
     if (fan != NULL)
      {
            
      if (string.isBlank(fanEmail))
      {
         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Email Cannot Be Blank.');
         ApexPages.addMessage(myMsg);
      }  
      else
      {
      if (fanEmail.equals(maskEmail(fanEmail)))
      {
         if (string.isBlank(fan_Email))
         
         fan.Email__c=fan_Email;  //user hits save button without making any changes
      }
      else
      {
         integer count=[select count() from fan__c where Email__c=:fanEmail];
        
         if (count > 0)
         {
           
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'This Fan Already Exists In The System.');
            ApexPages.addMessage(myMsg);
            return;
         }
         else
         {
            fan.Email__c=fanEmail;  //with changes.
         }
      }
      try
      {
         upsert fan;
      }
      catch(System.DmlException ex)
      {
            
        Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, 'Cannot Update the Record.. '+ex.getMessage()));
      }

    LoadMaskedValues();     
}

visual force page:
=================
 <apex:inputText value="{!fanEmail}" styleClass="form-control required"/>
  <apex:commandButton value="SAVE CHANGES" action="{!btn_profile_saveChanges}" styleClass="btn btn-success"/>

Thanks
krishna