Validating if UK postcodes 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 UK postcodes conform to the correct format.

How to do it...

Carry out the following step to create a validation rule:

  1. Navigate to the Accounts 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 UK Postcode in the Field Label textbox (upon clicking out of the text box, the name changes to Valid_UK_Postcode).
  4. Leave the Active checkbox checked.
  5. Optionally, set the Description field to This validation rule is for ensuring the entry of valid UK Postcodes.

    Note

    Navigate to the Error Condition Formula section.

  6. Paste the following code:
    AND(
      NOT(
        OR(
          REGEX( BillingPostalCode, "[a-zA-Z]\d\s\d[a-zA-Z]{2}"),
          REGEX( BillingPostalCode, "[a-zA-Z]\d\d\s\d[a-zA-Z]{2}"),
          REGEX( BillingPostalCode, "[a-zA-Z]{2}\d\s\d[a-zA-Z]{2}"),
          REGEX( BillingPostalCode, "[a-zA-Z]{2}\d\d\s\d[a-zA-Z]{2}"),
          REGEX( BillingPostalCode, "[a-zA-Z]{2}\d[a-zA-Z]\s\d[a-zA-Z]{2}"),
          REGEX( BillingPostalCode, "[a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]{2}")
        )
      ),
      OR( 
        UPPER( BillingCountry ) = "UK", 
        UPPER( BillingCountry ) = "U.K.",
        UPPER( BillingCountry ) = "UNITED KINGDOM"
      )
    )
  7. In the Error Message section, enter the text Please enter a valid UK Post code of the typical format: XXNN NYY.
  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 UK postcode 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.189.189.67