[SalesForce] Regex Validation rule for telephone number

I want to create a validation rule on Phone field(Custom) where system will be allowed to use + or 00 and then 7 to 26 digits(Numeric). Also in between if I enter any space or dash that should not be counted in this limit.

eg: +49 1521 4846987364 or 0049 1521 4846987364 System can save this number where it will read total characters as 16 excluding ( ) and +/00

+49 75681 94875419854719485473 or 0049 75681 94875419854719485473 system will throw an error here since total digits here are more than 26

+49 7382 or 0049 7382 system will throw an error here since total digits here are 6 ie <7.

Best Answer

I would recommend a two step approach. First keep your length restrictions on hold and go and find a well tested working solution for phone numbers. You don't have to reinvent the wheel:

try as formula then something like:

NOT(  
  REGEX( SamplePhone__c, "^\\+([0-9 ]+)$" ) 
  &&  LEN(  SUBSTITUTE(SamplePhone__c , " ", "")   ) < 26  
  &&  LEN(  SUBSTITUTE(SamplePhone__c , " ", "")   ) > 6 
)

Replace the regex to the one of your choice and use nested SUBSTITUE(...) to remove other character you don't want to count against your Min and Max.

The idea is to decouple the regex and the length checks and combine them logically.

Related Topic