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
Andrea BrumanAndrea Bruman 

pattern.compile US phone number

Hi,
I am trying to validate a phone number contains 10 numbers.  Salesforce reformats the number to include the parentheses and hyphen.  Here is what I have so far
if (newCase.NOI_Primary_Contact_Phone_Number__c <> null && newCase.NOI_Primary_Contact_Phone_Number__c <> '')
            {
                String strP = newCase.NOI_Primary_Contact_Phone_Number__c.trim();
                Pattern pceP = Pattern.compile('^(?=.*?[1-9])[0-9()-]+$');
                
                Matcher mceP = pceP.matcher(strP);
                if (mceP.matches() == false)                   
                {
                    isValid = false;
                    chkPhone = true;
                }
            }
It is not working for a phone number format that should work, example- (789) 456-1230
Help!
 
bhanu_prakashbhanu_prakash
Hi Andrea,
Mark as best answer, If it resloves !!​
AND(
  NOT(
    AND(
      LEN(Phone) == 14,
      LEFT(Phone,1) == '(',
      ISNUMBER(LEFT(RIGHT(Phone,13),3)),
      LEFT(RIGHT(Phone,10),1) == ')',
      LEFT(RIGHT(Phone,9),1) == ' ',
      ISNUMBER(LEFT(RIGHT(Phone,8),3)),
      LEFT(RIGHT(Phone,5),1) == '-',
      ISNUMBER(RIGHT(Phone,4))
    )
  ),
   ISNUMBER(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(Phone , ".", ''),"-",""),"+","")),
   OR(
    LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(Phone ,".",''),"-",""),"+",""))=10,
    AND(
      LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(Phone,".",''),"-",""),"+",""))=11,
      LEFT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(Phone ,".",''),"-",""),"+",""),1)="1"
    )
  )
)
And here is the formula to use to update the Phone field:
IF(
 LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE( Phone , ".", ''),"-",""),"+",""))= 11,
 "("&
 MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE( Phone , ".", ''),"-",""),"+",""),2,3)&
 ") "&
 MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE( Phone , ".", ''),"-",""),"+",""),5,3)&
 "-"&
 MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE( Phone , ".", ''),"-",""),"+",""),8,4),
 "("&
 MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE( Phone , ".", ''),"-",""),"+",""),1,3)&
 ") "&
 MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE( Phone , ".", ''),"-",""),"+",""),4,3)&
 "-"&
 MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE( Phone , ".", ''),"-",""),"+",""),7,4)
)

check these link for more info 
https://developer.salesforce.com/forums/?id=906F00000009FN3IAM (http://​https://developer.salesforce.com/forums/?id=906F00000009FN3IAM)

Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com​ (https://www.forcelearn.com)