Validating if US zip codes are well formed using a validation rule with REGEX

Accurate information and reports are vital in an effective Salesforce CRM system and without it, users and sales management cannot make informed decisions. One way to achieve accurate information for reporting is by ensuring that data is stored in a consistent manner.

In this recipe, we will create a validation rule to ensure that US zip codes conform to the correct format.

How to do it...

Carry out the following step to create a validation rule:

  1. Navigate to the account customization setup page, by going to Your Name | Setup | Customize | Accounts | Validation Rules.
  2. Click on New.

    Note

    Here, we are presented with the Account Validation Rule Edit page.

  3. Type Valid US Zip Code in the Field Label textbox (upon clicking out of the textbox, the name changes to Valid_US_Zip_Code).
  4. Leave the Active checkbox checked.
  5. Optionally, set the Description field to This validation rule is for ensuring the entry of valid US Zip codes.

    Note

    Navigate to the Error Condition Formula section.

  6. Paste the following code:
    AND(
      NOT( REGEX( BillingPostalCode, "\d{5}(-\d{4})?" ) ),
      OR( 
        UPPER( BillingCountry ) = "US", 
        UPPER( BillingCountry ) = "USA",
        UPPER( BillingCountry ) = "U.S.A.",
        UPPER( BillingCountry ) = "U.S.",
        UPPER( BillingCountry ) = "UNITED STATES"
       ) 
    )
  7. In the Error Message section, enter the text Please enter a valid US Zip code of the format: five digits as NNNNN or Zip + 4 as in NNNNN-NNNN.
  8. In the Error Location option, select Field.

    Note

    Here, we are presented with the Field selection pick list.

  9. Select the field Billing Zip/PostalCode (as shown):
    How to do it...
  10. Finally, click on Save.

How it works...

You can see how the entering of an invalid US zip code is validated and how the message appears within the edit page in the following screenshot:

How it works...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.20.68