+ Start a Discussion
Roy SinghRoy Singh 

how to write trigger on phone number validation

Hi All,
i want to write trigger on phone fields,
supposs user enter phone number like this:
004466554412-user input
its should be change like this:
+44 66 55 44 12
00 converts in +
after 2 digit its should be space


Thanks
Raj VakatiRaj Vakati
Use this sample code 

 
String uinp ='004466554412' ; 
if(uinp.startsWith('00')){
    String[] aa=uinp.split('(?<=\\G.{2})');
    System.debug('array'+aa);
    String finalStr = '+' ;
    for (integer i=1;i<aa.size();i++){
        finalStr =finalStr+' '+ aa[i]; 
    }
    System.debug('finalStr'+finalStr);
}

 
GhanshyamChoudhariGhanshyamChoudhari
try this code in anonymous execution window
 
string phone ='004466554412';
if(phone.startsWith('00')){   
    phone= phone.substring(2,phone.length());
    phone='+'+phone;
    phone = phone.substring(0, 3) + ' ' + phone.substring(4, phone.length());
    system.debug('phone2@@@'+phone);
}

 
Roy SinghRoy Singh
Hi Raj,
its working in  anonymous execution window but how to move this in Trigger ?
GhanshyamChoudhariGhanshyamChoudhari
trigger contactphonevalidation on Contact (before insert,before update) {
    for(Contact c:Trigger.new){
        string phone =c.Phone;
        if(phone.startsWith('00')){   
            phone= phone.substring(2,phone.length());
            phone='+'+phone;
            phone = phone.substring(0, 3) + ' ' + phone.substring(4, phone.length());
         	c.Phone=phone;            
        }
        
    }
    
}

 
Roy SinghRoy Singh
same thing i did but its not working
GhanshyamChoudhariGhanshyamChoudhari
please specify the error you are getting