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


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.

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.

String.replaceAll -
java.util.regex.Pattern -
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.

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]);
   alert('contact updated successfully');
  alert('Error : '+result);

The above code will replace all characters except numbers.

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

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.