[SalesForce] REGEX Pattern for “PO BOX”

I have a requirement to create a validation rule on custom object Address_vod__c. There is a picklist field in the object,'Type'. If 'Type' value is not 'mailing' then restrict to create or edit records for below strings.

  • PO Box
  • Post Office Box
  • POBox
  • PostOffice Box
  • PostOfficeBox
  • P.O. Box
  • P O Box

*strings are case insensitive

My below code fails for this value:

Post Office Box Pobox 5500 E 2nd St

Validation Rule :

AND(
   OR(REGEX(Name,"\\b(?i)(P *O * B *o *x*)\\b"),
      REGEX(Name,"\\b(P. *O. *Box)\\b")
    ),
    NOT( ISPICKVAL(Address_Type_abv__c,"Mailing"))
)

Best Answer

I would most likely go with the following REGEX as it should provide you with the most flexibility and capture case insensitive values as well. In addition, it will capture if they fully write out Post Office Box as well.

(?i)\b(?:p\.?\s*o\.?|post\s+office)\s+box\b
Related Topic