+ Start a Discussion
Vasavi VajinepalliVasavi Vajinepalli 

Removing '-' from phone number field which is of type String

Hi,

I've a field for Phone number of type String. And the format of it would be 111-222-3333. I want to remove the '-' from it and it should look like 1112223333. Please suggest me a validation rule or formula to achieve this.

Thanks,
vasavi 
GauravTrivediGauravTrivedi
Hey Vasavi,

Use the following code it will work for you:
String s1 = '111-222-3333';
String s2;
List<String> l = s1.split('-');
for(String s : l){
    s2 +=s; 
    S2 = s2.remove('null');
}
System.debug('String : '+s2);
Let me know if it helped you. 
 
Vasavi VajinepalliVasavi Vajinepalli
Hi Gaurav,

Thanks for your post. But i would like to have logic as a validation rule or formula. Could you help me in that regard?

Thanks,
vasavi 
GauravTrivediGauravTrivedi
For that create formula field as a TEXT and put the following formula

SUBSTITUTE( phoneNumber__c, '-', '')

hope it will work.
Vasavi VajinepalliVasavi Vajinepalli
Can you please elobarate more about the solution. When i try to create validation rule with your suggestion, it is throwing an error.

Thanks,
vasavi
GauravTrivediGauravTrivedi
This is not validation rule.
You need to create formula field on that object and select data type as TEXT and in advance formula editor put this formula 
SUBSTITUTE( phoneNumber__c, '-', ''). Now use this new field whereever you want to use.