Newer Version Available

This content describes an older version of this product. View Latest

Sample Account Address Validation Rules

Available in: both Salesforce Classic and Lightning Experience
Available in: Contact Manager, Group, Professional, Enterprise, Performance, Unlimited, Developer, and Database.com Editions

For more information on any of the formula functions used in these examples, see Formula Operators and Functions.

Canadian Billing Postal Code

Field Value
Description: Validates that the account Billing Zip/Postal Code is in the correct format if Billing Country is Canada.
Formula:
1AND(
2OR(BillingCountry = "CAN", BillingCountry = "CA", BillingCountry = "Canada"),
3NOT(REGEX(BillingPostalCode, "((?i)[ABCEGHJKLMNPRSTVXY]\\d[A-Z]?\\s?\\d[A-Z]\\d)?"))
4)
Error Message: Canadian postal code must be in A9A 9A9 format.
Error Location: Billing Zip/Postal Code

Billing Zip Code Is in Billing State

Field Value
Description: Validates that the account Billing Zip/Postal Code is valid by looking up the first five characters of the value in a custom object called Zip_Code__c that contains a record for every valid zip code in the US. If the zip code is not found in the Zip_Code__c object, or the Billing State does not match the corresponding State_Code__c in the Zip_Code__c object, an error is displayed.
Formula:
1VLOOKUP(
2$ObjectType.Zip_Code__c.Fields.City__c ,
3$ObjectType.Zip_Code__c.Fields.Name ,
4LEFT(BillingPostalCode,5)) <> BillingCity
Error Message: Billing Zip Code does not exist in specified Billing State.
Error Location: Billing Zip/Postal Code

US Billing Zip Code

Field Value
Description: Validates that the account Billing Zip/Postal Code is in 99999 or 99999-9999 format if Billing Country is USA or US.
Formula:
1AND(
2OR(BillingCountry = "USA", BillingCountry = "US"),
3NOT(REGEX(BillingPostalCode, "\\d{5}(-\\d{4})?"))
4)

This example uses the REGEX function; see Shipping Zip Code if you are not familiar with regular expressions.

Note

Error Message: Zip code must be in 99999 or 99999-9999 format.
Error Location: Billing Zip/Postal Code

Shipping Zip Code

Field Value
Description: Validates that the account Shipping Zip/Postal Code is in 99999 or 99999-9999 format if Shipping Country is USA or blank.
Formula:
1AND(
2 OR(ShippingCountry = "USA", ISBLANK(ShippingCountry)),
3  OR(
4   AND(LEN(ShippingPostalCode) <>5, 
5      LEN(ShippingPostalCode) <> 10),
6   NOT(CONTAINS("0123456789", 
7      LEFT( ShippingPostalCode, 1))),
8   NOT(CONTAINS("0123456789", 
9      MID( ShippingPostalCode , 2, 1))),
10   NOT(CONTAINS("0123456789", 
11      MID( ShippingPostalCode , 3, 1))),
12   NOT(CONTAINS("0123456789", 
13      MID( ShippingPostalCode , 4, 1))),
14   NOT(CONTAINS("0123456789", 
15      MID( ShippingPostalCode , 5, 1))),
16   AND(
17    LEN(ShippingPostalCode) = 10,
18   OR(
19    MID( ShippingPostalCode , 6, 1) <> "-",
20    NOT(CONTAINS("0123456789", 
21      MID( ShippingPostalCode , 7, 1))),
22    NOT(CONTAINS("0123456789", 
23      MID( ShippingPostalCode , 8, 1))),
24    NOT(CONTAINS("0123456789", 
25      MID( ShippingPostalCode , 9, 1))),
26    NOT(CONTAINS("0123456789", 
27      MID( ShippingPostalCode , 10, 1)))
28   )
29  )
30 )
31)

This example interprets a blank country as US. To use this example with other countries, remove the clause that checks the length of the country field. Also, validation rule criteria are case sensitive, so this rule is only enforced when the country is blank or “USA” in all capital letters. The rule is not enforced when the country is “usa.”

Note

You can also validate zip codes using a regular expression; for an example of a formula using a regular expression, see REGEX.

Tip

Error Message: Zip code must be in 99999 or 99999-9999 format.
Error Location: Shipping Zip/Postal Code

Valid Billing State (US)

Field Value
Description: Validates that the account Billing State/Province is a valid two-character abbreviation if Billing Country is US, USA, or blank.
Formula:
1AND (
2OR(BillingCountry = "US", BillingCountry="USA", ISBLANK(BillingCountry)),
3OR(
4LEN(BillingState) < 2,
5NOT(
6CONTAINS("AL:AK:AZ:AR:CA:CO:CT:DE:DC:FL:GA:HI:ID:" &
7"IL:IN:IA:KS:KY:LA:ME:MD:MA:MI:MN:MS:MO:MT:NE:NV:NH:" &
8"NJ:NM:NY:NC:ND:OH:OK:OR:PA:RI:SC:SD:TN:TX:UT:VT:VA:" &
9"WA:WV:WI:WY", BillingState)
10)))

This example interprets a blank country as US. To use this example with other countries, remove the clause that checks the length of the country field. Also, validation rule criteria are case sensitive, so this rule is only enforced when the country is blank or “USA” in all capital letters. The rule is not enforced when the country is “usa.”

Note

Error Message: A valid two-letter state code is required.
Error Location: Billing State/Province

Valid Billing Province (Canada)

Field Value
Description: Validates that the account Billing State/Province is a valid two-character abbreviation if Billing Country is CA or CAN.
Formula:
1AND (
2   OR(BillingCountry = "CA", BillingCountry="CAN"),
3   OR(
4   LEN(BillingState) < 2,
5   NOT(
6      CONTAINS("AB:BC:MB:NB:NL:NT:NS:NU:ON:PC:QC:SK:YT", BillingState)
7)))
Error Message: A valid two-letter province code is required.
Error Location: Billing State/Province

Valid Shipping State

Field Value
Description: Validates that the account Shipping State/Province is a valid two-character abbreviation if Shipping Country is US, USA, or blank.
Formula:
1AND (
2OR(ShippingCountry = "US", ShippingCountry="USA", ISBLANK(ShippingCountry)),
3OR(
4LEN(ShippingState) < 2,
5NOT(
6CONTAINS("AL:AK:AZ:AR:CA:CO:CT:DE:DC:FL:GA:HI:ID:" &
7"IL:IN:IA:KS:KY:LA:ME:MD:MA:MI:MN:MS:MO:MT:NE:NV:NH:" &
8"NJ:NM:NY:NC:ND:OH:OK:OR:PA:RI:SC:SD:TN:TX:UT:VT:VA:" &
9"WA:WV:WI:WY", ShippingState)
10)))

This example interprets a blank country as US. To use this example with other countries, remove the clause that checks the length of the country field. Also, validation rule criteria are case sensitive, so this rule is only enforced when the country is blank or “USA” in all capital letters. The rule is not enforced when the country is “usa.”

Note

Error Message: A valid two-letter state abbreviation is required.
Error Location: Shipping State/Province

Valid Shipping Province (Canada)

Field Value
Description: Validates that the account Shipping State/Province is a valid two-character abbreviation, if Billing Country is CA or CAN.
Formula:
1AND (
2OR(ShippingCountry = "CA", ShippingCountry="CAN"),
3OR(
4LEN(ShippingState) < 2,
5NOT(
6CONTAINS("AB:BC:MB:NB:NL:NT:NS:NU:ON:PC:QC:SK:YT", ShippingState)
7)))
Error Message: A valid two-letter province abbreviation is required.
Error Location: Shipping State/Province

Valid Billing Country

Field Value
Description: Validates that the account Billing Country is a valid ISO 3166 two-letter code.
Formula:
1OR(
2LEN(BillingCountry) = 1,
3NOT(
4CONTAINS(
5"AF:AX:AL:DZ:AS:AD:AO:AI:AQ:AG:AR:AM:" &
6"AW:AU:AZ:BS:BH:BD:BB:BY:BE:BZ:BJ:BM:BT:BO:" &
7"BA:BW:BV:BR:IO:BN:BG:BF:BI:KH:CM:CA:CV:KY:" &
8"CF:TD:CL:CN:CX:CC:CO:KM:CG:CD:CK:CR:CI:HR:" &
9"CU:CY:CZ:DK:DJ:DM:DO:EC:EG:SV:GQ:ER:EE:ET:FK:" &
10"FO:FJ:FI:FR:GF:PF:TF:GA:GM:GE:DE:GH:GI:GR:GL:" &
11"GD:GP:GU:GT:GG:GN:GW:GY:HT:HM:VA:HN:HK:HU:" &
12"IS:IN:ID:IR:IQ:IE:IM:IL:IT:JM:JP:JE:JO:KZ:KE:KI:" &
13"KP:KR:KW:KG:LA:LV:LB:LS:LR:LY:LI:LT:LU:MO:MK:" &
14"MG:MW:MY:MV:ML:MT:MH:MQ:MR:MU:YT:MX:FM:MD:MC:" &
15"MC:MN:ME:MS:MA:MZ:MM:MA:NR:NP:NL:AN:NC:NZ:NI:" &
16"NE:NG:NU:NF:MP:NO:OM:PK:PW:PS:PA:PG:PY:PE:PH:" &
17"PN:PL:PT:PR:QA:RE:RO:RU:RW:SH:KN:LC:PM:VC:WS:" &
18"SM:ST:SA:SN:RS:SC:SL:SG:SK:SI:SB:SO:ZA:GS:ES:" &
19"LK:SD:SR:SJ:SZ:SE:CH:SY:TW:TJ:TZ:TH:TL:TG:TK:" &
20"TO:TT:TN:TR:TM:TC:TV:UG:UA:AE:GB:US:UM:UY:UZ:" &
21"VU:VE:VN:VG:VI:WF:EH:YE:ZM:ZW",
22BillingCountry)))
Error Message: A valid two-letter country code is required.
Error Location: Billing Country