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
advlgxpmadvlgxpm 

Phone number validation/formatting

Is there a prescribed way to manage the saving of phone numbers in a desired format? 5555551212 or 555 555-1212 would automatically be saved as (555) 555-1212 etc.? I notice on a user form, the field has built in phone formatting while typing. Any way to do that?
RickyGRickyG
I guess you could use Javascript - not sure if Javascripts supports regular expressions or not.  In any event, you would have to process each keystroke, which would probably get kind of messy, as well as potentially annoying, as far as I can tell.

User education might be a better way to go.  Hope this helps.
advlgxpmadvlgxpm
User education... why didn't I think of that. :smileywink:  That always works so well...... :smileyvery-happy:

I'll look into the regex. Thanks. Was hoping I could craft a formula in a workflow or something where if phonefieldx changed, parse the value and format it properly.
RickyGRickyG
Ooops - my bad.  I misread your question and thought you wanted to do this as the user was entering the phone number.

Of course, you have the Phone data type, which does pretty close to what you want.  If not, you could use a formula using our regex function to format it.

My apologies - how foolish of me to suggest user education!  :smileytongue:
advlgxpmadvlgxpm
NP..... I assume a lot fewer of us would have jobs if user education was effective. :smileytongue:
HLSHLS

Hi Rick,

 

What kind of script have you implemented in order to format telephone numbers?

 

Thanks in advance!

 

HLS

PerGeertPerGeert

I am trying the following:

First I have a class with a method to format the phone number depending on country in order to allow the user simply to enter digits:

public class Utils { // Collection of general purpose functions public static string formatPhone(string phone, string country) { // Format a phone number according to the country // In general two format may be expected, with and without country code // e.g. '11 2222-3333' and +55 11 2222-3333' for Brazil // If phone does not apply to country (i.e. wrong number of digits), // phone is returned string phonedigits; string nondigits = '[^0-9]'; if (phone == null || country == null) return phone; if (phone == '' || country == '') return phone; if (country == 'Brazil') { phonedigits = phone.replaceAll(nondigits,''); if (phonedigits.length() == 10) return phonedigits.substring(0,2) + ' ' + phonedigits.substring(2,6) + '-' + phonedigits.substring(6,10); if (phonedigits.length() == 12) { if (phonedigits.substring(0,2) == '55') { return '+55 ' + phonedigits.substring(2,4) + ' ' + phonedigits.substring(4,8) + '-' + phonedigits.substring(8,12); } } } else { if (country == 'USA' || country == 'US' || country == 'United States' || country == 'Canada') { phonedigits = phone.replaceAll(nondigits,''); if (phonedigits.length() == 10) return phonedigits.substring(0,3) + '-' + phonedigits.substring(3,6) + '-' + phonedigits.substring(6,10); if (phonedigits.length() == 11) { if (phonedigits.substring(0,1) == '1') { return '+1-' + phonedigits.substring(1,4) + '-' + phonedigits.substring(4,7) + '-' + phonedigits.substring(7,11); } } } } // If we get here it is either an error or an unsupported country return phone; } }

 

I call this method in a before trigger, e.g.:

trigger tgrPhoneFormat on Account (before update, before insert) { // Format phone number before insert/update // Validation rule checks if the format is acceptable for (Account ac : Trigger.new) { ac.phone = Utils.formatPhone(ac.phone, ac.primarycountry__c); } }

 

Finally a validation rule catch errors, where required:

AND ( ISPICKVAL( PrimaryCountry__c , "Brazil"), NOT ( OR ( LEN ( Phone ) = 0, REGEX( Phone, "\\d{2}\\s\\d{4}-\\d{4}"), REGEX( Phone, "\\+55\\s\\d{2}\\s\\d{4}-\\d{4}") ) ) )

 

 

One problem I am having is that the standard phone formatting routine changes the value if the phone number starts with 1 and has 11 digits. E.g. typing '12345678901' is formatted (by standard routine) to '(234) 567-8901' before my routine gets control. No good!

 

Regards,

Per

mluisimluisi
How can I write just this portion of your code as a field update please? 

if (phonedigits.length() == 10) return phonedigits.substring(0,3) + '-' + phonedigits.substring(3,6) + '-' + phonedigits.substring(6,10)

I am receiving syntax errors... :'(