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
Tejas Wadke 5Tejas Wadke 5 

Replacing non-numeric values in phone fields

Hello,

I want to implement a functionality
Where on a button click Phone number field will eliminate non-numeric values
There are phone numbers like 030 / 3906 345-61 ,07272 / 7704 1245 O etc,I want to eliminate all non-numeric values in these numbers.
Any sample code snippet on this.

Thanks,
Tejas
Kevin CrossKevin Cross
Good morning, you could try String.replaceAll( regExp, replacement ) where regExp is a Java-style regular expression and replacement is the string to insert instead, which in your case would be an empty string.  Note because you are removing characters in the middle, you also may want to do a sweep for double or triple space combinations after replacement.

References:
String.replaceAll - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_replaceAll
java.util.regex.Pattern - http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
Bhanu MaheshBhanu Mahesh
Hi Tejas,

Create a detail page button and select Onclick javascript as the source of the button and add the below code.
I have taken contact for my example. Please modify as per your requirement.
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var contact = new sforce.SObject("Contact");
contact.Id = '{!Contact.Id}';
var phone = '{!Contact.Phone_Custom__c}';
phone = phone.replace(/\D/g,'');
contact.Phone_Custom__c = phone;
var result = sforce.connection.update([contact]);
if(result[0].getBoolean("success"))
{
   alert('contact updated successfully');
   window.location.reload();
}
else{
  alert('Error : '+result);
}

The above code will replace all characters except numbers.

Mark this as "SOLVED" if your question is answered.

Regards,
Bhanu Mahesh Gadi
Kevin CrossKevin Cross
Good catch Bhanu, the Author probably is looking for JavaScript and not Apex.  I thought I clicked under the Apex Code Development section, but I must not have.  Tejas, the code I pointed you to is for Apex solution, so please forgive the confusion.