Examples of Validation Rules
Examples of Validation Rules
Summary Review examples of validation rules for various types of apps that you can use and modify for your own
purposes. Validation rules verify that the data a user enters in a record meets the standards you specify
Review examples of
before the user can save the record.
validation rules for various
Use the following samples for validation rules in Salesforce and Salesforce AppExchange apps, including:
types of apps that you can
use and modify for your own
purposes. Validation rules Sample Account Address Validation Rules
verify that the data a user
For more information on any of the formula functions used in these examples, see Formula Operators and
enters in a record meets the
Functions.
standards you specify before
the user can save the record.
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:
AND(
OR(BillingCountry = "CAN", BillingCountry = "CA",
BillingCountry = "Canada"),
NOT(REGEX(BillingPostalCode,
"((?i)[ABCEGHJKLMNPRSTVXY]\\d[A-Z]?\\s?\\d[A-Z]\\d)?"))
)
Formula:
VLOOKUP(
$ObjectType.Zip_Code__c.Fields.City__c ,
$ObjectType.Zip_Code__c.Fields.Name ,
LEFT(BillingPostalCode,5)) <> BillingCity
Error Message: Billing Zip Code does not exist in specified Billing State.
Formula:
AND(
OR(BillingCountry = "USA", BillingCountry =
"US"),
NOT(REGEX(BillingPostalCode, "\\d{5}(-\\d{4})?"))
)
Note: This example uses the REGEX function; see Shipping Zip
Code if you are not familiar with regular expressions.
2
Examples of Validation Rules Sample Account Address Validation Rules
Formula:
AND(
OR(ShippingCountry = "USA",
ISBLANK(ShippingCountry)),
OR(
AND(LEN(ShippingPostalCode) <>5,
LEN(ShippingPostalCode) <> 10),
NOT(CONTAINS("0123456789",
LEFT( ShippingPostalCode, 1))),
NOT(CONTAINS("0123456789",
MID( ShippingPostalCode , 2, 1))),
NOT(CONTAINS("0123456789",
MID( ShippingPostalCode , 3, 1))),
NOT(CONTAINS("0123456789",
MID( ShippingPostalCode , 4, 1))),
NOT(CONTAINS("0123456789",
MID( ShippingPostalCode , 5, 1))),
AND(
LEN(ShippingPostalCode) = 10,
OR(
MID( ShippingPostalCode , 6, 1) <> "-",
NOT(CONTAINS("0123456789",
MID( ShippingPostalCode , 7, 1))),
NOT(CONTAINS("0123456789",
MID( ShippingPostalCode , 8, 1))),
NOT(CONTAINS("0123456789",
MID( ShippingPostalCode , 9, 1))),
NOT(CONTAINS("0123456789",
MID( ShippingPostalCode , 10, 1)))
)
)
)
)
Note: 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.”
Tip: You can also validate zip codes using a regular expression; for an
example of a formula using a regular expression, see REGEX.
3
Examples of Validation Rules Sample Account Address Validation Rules
Formula:
AND (
OR(BillingCountry = "US", BillingCountry="USA",
ISBLANK(BillingCountry)),
OR(
LEN(BillingState) < 2,
NOT(
CONTAINS("AL:AK:AZ:AR:CA:CO:CT:DE:DC:FL:GA:HI:ID:"
&
"IL:IN:IA:KS:KY:LA:ME:MD:MA:MI:MN:MS:MO:MT:NE:NV:NH:"
&
"NJ:NM:NY:NC:ND:OH:OK:OR:PA:RI:SC:SD:TN:TX:UT:VT:VA:"
&
"WA:WV:WI:WY", BillingState)
)))
4
Examples of Validation Rules Sample Account Address Validation Rules
Formula:
AND (
OR(BillingCountry = "CA", BillingCountry="CAN"),
OR(
LEN(BillingState) < 2,
NOT(
CONTAINS("AB:BC:MB:NB:NL:NT:NS:NU:ON:PC:QC:SK:YT",
BillingState)
)))
Formula:
AND (
OR(ShippingCountry = "US", ShippingCountry="USA",
ISBLANK(ShippingCountry)),
OR(
LEN(ShippingState) < 2,
NOT(
CONTAINS("AL:AK:AZ:AR:CA:CO:CT:DE:DC:FL:GA:HI:ID:" &
"IL:IN:IA:KS:KY:LA:ME:MD:MA:MI:MN:MS:MO:MT:NE:NV:NH:"
&
"NJ:NM:NY:NC:ND:OH:OK:OR:PA:RI:SC:SD:TN:TX:UT:VT:VA:"
&
"WA:WV:WI:WY", ShippingState)
)))
Note: 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.”
5
Examples of Validation Rules Sample Account Address Validation Rules
Formula:
AND (
OR(ShippingCountry = "CA", ShippingCountry="CAN"),
OR(
LEN(ShippingState) < 2,
NOT(
CONTAINS("AB:BC:MB:NB:NL:NT:NS:NU:ON:PC:QC:SK:YT",
ShippingState)
)))
6
Examples of Validation Rules Sample Account Validation Rules
Formula:
OR(
LEN(BillingCountry) = 1,
NOT(
CONTAINS(
"AF:AX:AL:DZ:AS:AD:AO:AI:AQ:AG:AR:AM:" &
"AW:AU:AZ:BS:BH:BD:BB:BY:BE:BZ:BJ:BM:BT:BO:" &
"BA:BW:BV:BR:IO:BN:BG:BF:BI:KH:CM:CA:CV:KY:" &
"CF:TD:CL:CN:CX:CC:CO:KM:CG:CD:CK:CR:CI:HR:" &
"CU:CY:CZ:DK:DJ:DM:DO:EC:EG:SV:GQ:ER:EE:ET:FK:" &
"FO:FJ:FI:FR:GF:PF:TF:GA:GM:GE:DE:GH:GI:GR:GL:" &
"GD:GP:GU:GT:GG:GN:GW:GY:HT:HM:VA:HN:HK:HU:" &
"IS:IN:ID:IR:IQ:IE:IM:IL:IT:JM:JP:JE:JO:KZ:KE:KI:" &
"KP:KR:KW:KG:LA:LV:LB:LS:LR:LY:LI:LT:LU:MO:MK:" &
"MG:MW:MY:MV:ML:MT:MH:MQ:MR:MU:YT:MX:FM:MD:MC:" &
"MC:MN:ME:MS:MA:MZ:MM:MA:NR:NP:NL:AN:NC:NZ:NI:" &
"NE:NG:NU:NF:MP:NO:OM:PK:PW:PS:PA:PG:PY:PE:PH:" &
"PN:PL:PT:PR:QA:RE:RO:RU:RW:SH:KN:LC:PM:VC:WS:" &
"SM:ST:SA:SN:RS:SC:SL:SG:SK:SI:SB:SO:ZA:GS:ES:" &
"LK:SD:SR:SJ:SZ:SE:CH:SY:TW:TJ:TZ:TH:TL:TG:TK:" &
"TO:TT:TN:TR:TM:TC:TV:UG:UA:AE:GB:US:UM:UY:UZ:" &
"VU:VE:VN:VG:VI:WF:EH:YE:ZM:ZW",
BillingCountry)))
7
Examples of Validation Rules Sample Account Validation Rules
Formula:
AND(
ISBLANK(AccountNumber),
NOT(ISNUMBER(AccountNumber))
)
Formula:
AND(
ISBLANK(AccountNumber),
LEN(AccountNumber) <> 7
)
Formula:
OR(
AnnualRevenue < 0,
AnnualRevenue > 100000000000
)
8
Examples of Validation Rules Sample Call Center Validation Rules
Formula:
AND(
ISPICKVAL( Reason, "Other" ),
ISBLANK(Other_Reason__c)
)
Formula:
AND(
ISCHANGED( Status ),
NOT(ISPICKVAL(PRIORVALUE( Status ), "New")),
ISPICKVAL( Status, "New")
)
9
Examples of Validation Rules Sample Call Center Validation Rules
Formula:
AND(
ISCHANGED( Status ),
OR(
ISPICKVAL(PRIORVALUE( Status ), "Closed"),
ISPICKVAL(PRIORVALUE( Status ),
"Closed in SSP")),
NOT( ISPICKVAL( Status, "Re-Opened"))
)
Formula:
Case.IsClosed = true
Formula:
CompletionDate >= Case.CreatedDate &&
CompletionDate <= Case.ClosedDate
Error Message: The milestone Completion Date must occur after the date the case was
created and before the case was closed.
10
Examples of Validation Rules Sample Community Validation Rules
Formula:
OR(CONTAINS(Title, 'darn'), CONTAINS(Body,
'darn'))
Formula:
OR(CONTAINS(Body, 'darn'), CONTAINS(Body,
'dang'))
Formula:
OR(CONTAINS(Title, 'darn'), CONTAINS(Body,
'darn'))
11
Examples of Validation Rules Sample Contact Validation Rules
Formula:
OR(CONTAINS(CommentBody , 'darn'),
CONTAINS(CommentBody, 'dang'))
Formula:
OR(
ISBLANK( MailingStreet ),
ISBLANK( MailingCity ),
ISBLANK( MailingCountry )
)
Formula:
ISBLANK( MailingStreet )
12
Examples of Validation Rules Sample Contact Validation Rules
Formula:
AND(
OR(MailingCountry = "USA", ISBLANK(MailingCountry)),
OR(
AND(LEN(MailingPostalCode) <>5,
LEN(MailingPostalCode) <> 10),
NOT(CONTAINS("0123456789",
LEFT( MailingPostalCode, 1))),
NOT(CONTAINS("0123456789",
MID( MailingPostalCode , 2, 1))),
NOT(CONTAINS("0123456789",
MID( MailingPostalCode , 3, 1))),
NOT(CONTAINS("0123456789",
MID( MailingPostalCode , 4, 1))),
NOT(CONTAINS("0123456789",
MID( MailingPostalCode , 5, 1))),
AND(
LEN(MailingPostalCode) = 10,
OR(
MID( MailingPostalCode , 6, 1) <> "-",
NOT(CONTAINS("0123456789",
MID( MailingPostalCode , 7, 1))),
NOT(CONTAINS("0123456789",
MID( MailingPostalCode , 8, 1))),
NOT(CONTAINS("0123456789",
MID( MailingPostalCode , 9, 1))),
NOT(CONTAINS("0123456789",
MID( MailingPostalCode , 10, 1)))
)
)
)
)
Note: 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.”
Tip: You can also validate zip codes using a regular expression; for an
example of a formula using a regular expression, see REGEX.
13
Examples of Validation Rules Sample Cross Object Validation Rules
Formula:
LEFT(Phone, 1) <> "+"
Formula:
NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))
Field Value
Description: Prevents users from saving software products with
a discount over 10 percent.
14
Examples of Validation Rules Sample Cross Object Validation Rules
Field Value
Formula:
AND(Line_Discount__c > 0.10,
ISPICKVAL(Product2.Family,
"Software"))
Consulting Discounts
Field Value
Description: Prevents users from saving consulting products
with a discount over 15 percent.
Formula:
AND(Line_Discount__c > 0.15,
ISPICKVAL(Product2.Family,
"Consulting"))
Training Discounts
Field Value
Description: Prevents users from saving training products with
a discount over 20 percent.
Formula:
AND(Line_Discount__c > 0.20,
ISPICKVAL(Product2.Family,
"Training"))
15
Examples of Validation Rules Sample Cross Object Validation Rules
Field Value
Description: Prevents users from editing opportunity products
after an opportunity is closed. Create the following
validation rule example on opportunity products.
Formula:
OR(ISPICKVAL(Opportunity.StageName,
"Closed Won"),
ISPICKVAL(Opportunity.StageName,
"Closed Lost"))
Field Value
Description: Prevents users from deleting opportunity products
after an opportunity is closed. Create the following
validation rule example on opportunities. It uses a
custom roll-up summary field on opportunities that
counts the number of opportunity products on an
opportunity.
Formula:
AND(OR(ISPICKVAL(StageName,
"Closed Won"),
ISPICKVAL(StageName, "Closed
Lost")), Number_of_Line_Items__c
<
PRIORVALUE(Number_of_Line_Items__c)
)
16
Examples of Validation Rules Sample Date Validation Rules
Field Value
Formula:
Account.Allowed_Support__c =
FALSE
Formula:
AND(Contact.Not_Longer_With_Company__c,
NOT(IsClosed))
Error Message: Unable to save this case because the related contact
is no longer with the company. To continue, choose
another contact.
17
Examples of Validation Rules Sample Date Validation Rules
Formula:
CASE(MOD( My_Date__c - DATE(1900, 1, 7), 7),
0, 0,
6, 0,
1) = 0
Formula:
CASE( MOD( My_Date__c - DATE(1900, 1, 7), 7),
0, 1,
6, 1,
0) = 0
Formula:
OR (
YEAR( My_Date__c ) <> YEAR ( TODAY() ),
MONTH( My_Date__c ) <> MONTH ( TODAY() )
)
18
Examples of Validation Rules Sample Date Validation Rules
Formula:
DAY(My_Date__c) <>
IF(Month(My_Date__c)=12, 31,
DAY(DATE(YEAR(My_Date__c),MONTH(My_Date__c)+1,1)
- 1))
Formula:
Followup_Date__c - TODAY() > 365
19
Examples of Validation Rules Sample Number Validation Rules
Formula:
DAY( Begin_Date__c ) > 15
Error Message: Begin Date cannot be after the 15th day of month.
Formula:
Begin_Date__c > End_Date__c
Formula:
Expiration_Date__c < CloseDate
20
Examples of Validation Rules Sample Number Validation Rules
Formula:
Monday_Hours__c +
Tuesday_Hours__c +
Wednesday_Hours__c +
Thursday_Hours__c +
Friday_Hours__c > 40
Formula:
Hours_Worked__c < 0
Formula:
OR(
Ark_Passengers__c < 0,
MOD( Ark_Passengers__c, 2) <> 0
)
21
Examples of Validation Rules Sample Number Validation Rules
Formula:
OR(
Socks_Found__c < 0,
MOD( Socks_Found__c, 2) = 0
)
Formula:
MOD( Multiple_of_5__c, 5) <> 0
Formula:
FLOOR( My_Integer__c) <> My_Integer__c
22
Examples of Validation Rules Sample Opportunity Management Validation Rules
Formula:
ABS( Volume__c) > 50
Formula:
(Salary_Max__c - Salary_Min__c) > 20000
Error Message: Salary range must be within $20,000. Adjust the Salary Max or Salary Min
values.
Formula:
OR(
Mix_Pct__c > 1.0,
Mix_Pct__c < 0.0
)
23
Examples of Validation Rules Sample Opportunity Management Validation Rules
Formula:
AND (
OR (
ISPICKVAL(StageName, "Closed Won"),
ISPICKVAL(StageName, "Negotiation/Review")),
ISBLANK(Delivery_Date__c)
)
Formula:
AND(
OR (
ISNEW(),
ISCHANGED( CloseDate )),
CloseDate < DATE( YEAR(TODAY()),
MONTH(TODAY()), 1) )
24
Examples of Validation Rules Sample Opportunity Management Validation Rules
Formula:
SampleDate < TODAY()
Discounts on Opportunities
Field Value
Description: Validates that a custom discount percent field is between 0 and 40%.
Formula:
AND(
OR(
ISPICKVAL(StageName,"Closed Won"),
ISPICKVAL(StageName,"Closed Lost")),
(Amount > 50000),
NOT(ISPICKVAL(Approval_Status__c ,"Approved")))
Error Message: All high-value opportunities must be approved for closure. Click the
Request Close button.
25
Examples of Validation Rules Sample Opportunity Management Validation Rules
Formula:
OR(
Amount < 0,
Amount > 10000000
)
Formula:
NOT(OR(ISNEW(),HasOpportunityLineItem))
Error Message: You must add products to this opportunity before saving.
26
Examples of Validation Rules Sample Opportunity Management Validation Rules
Formula:
AND (
CASE( StageName,
"Value Proposition", 1,
"Id. Decision Makers", 1,
"Perception Analysis", 1,
"Proposal/Price Quote", 1,
"Negotiation/Review", 1,
"Closed Won", 1,
0) = 1,
NOT(HasOpportunityLineItem)
)
Error Message: Opportunity products are required to advance beyond the Needs Analysis
stage.
Formula:
FIND( " - ", Name ) = 0
27
Examples of Validation Rules Sample Opportunity Management Validation Rules
Formula:
AND(
Accepted_Flag__c,
OR ( ISPICKVAL( StageName, "Stage 1"),
ISPICKVAL( StageName, "Stage 2"))
)
Formula:
AND (
ISPICKVAL( StageName, "Closed Won"),
Probability <> 1)
Formula:
AND (
ISPICKVAL( StageName, "Closed Lost"),
Probability <> 0)
28
Examples of Validation Rules Sample Quote Validation Rules
Formula:
AND(ISPICKVAL(StageName, "Closed Won"),
ISNULL(Project_Start_Date__c))
Formula:
Discount > .40
Error Message: The discount on this quote line item cannot exceed 40%.
29
Examples of Validation Rules Sample User, Role, and Profile Validation Rules
Formula:
Discount_Percent__c >
VLOOKUP($ObjectType.Role_Limits__c.Fields.Limit__c,
$ObjectType.Role_Limits__c.Fields.Name,
$UserRole.Name)
Error Message: Discount (%) exceeds limit allowed for your role.
Formula:
Expense_Amount__c > $User.Max_Allowed_Expense__c
Formula:
AND(
ISCHANGED( Personal_Goal__c ),
Owner <> $User.Id
)
30
Examples of Validation Rules Miscellaneous Sample Validation Rules
Formula:
AND( ISCHANGED( Personal_Goal__c ), Owner <>
$User.Id, $Profile.Name <>
"Custom: System Admin" )
Error Message: Only record owner or administrator can change Personal Goal.
Formula:
AND(
OR (
ISNEW(),
ISCHANGED( CloseDate )),
CloseDate < DATE( YEAR(TODAY()),
MONTH(TODAY()), 1),
$Profile.Name <> "Custom: System Admin"
)
31
Examples of Validation Rules Miscellaneous Sample Validation Rules
Formula:
PRIORVALUE(Commit_Amount__c) > Commit_Amount__c
Formula:
AND(
MailingState = "CA",
NOT(REGEX(Drivers_License__c, "([A-Z]\\d{7})?"))
)
Formula:
AND(
NOT( I_accept_terms__c ),
Number_of_Days__c > $User.PTO_Balance__c
)
Error Message: Request will cause a negative PTO balance. You must accept Negative
PTO Balance terms.
32
Examples of Validation Rules Miscellaneous Sample Validation Rules
Formula:
AND(
NOT( ISNEW() ),
ISCHANGED( Guaranteed_Rate__c )
)
Formula:
NOT(
OR(
ISBLANK(Social_Security_Number__c),
REGEX( Social_Security_Number__c ,
"[0-9]{3}-[0-9]{2}-[0-9]{4}")
)
)
33
Examples of Validation Rules Miscellaneous Sample Validation Rules
Valid Currency
Field Value
Description: Validates selected currency against an explicit subset of active currencies
in your organization using the Currency picklist. Use this example if
you only allow some of the active currencies in your organization to be
applied to certain types of records.
Formula:
CASE(CurrencyIsoCode,
"USD", 1,
"EUR", 1,
"GBP", 1,
"JPY", 1,
0) = 0
Formula:
NOT( REGEX( Credit_Card_Number__c ,
"(((\\d{4}-){3}\\d{4})|\\d{16})?"))
34
Examples of Validation Rules Miscellaneous Sample Validation Rules
Valid IP Address
Field Value
Description: Ensures that a custom field called IP Address is in the correct format,
four 3-digit numbers (0-255) separated by periods.
Formula:
NOT(
REGEX( IP_Address__c,
"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.)
{3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" ))
Error Message: Error: IP Address must be in form 999.999.999.999 where each part is
between 0 and 255.
Website Extension
Field Value
Description: Validates a custom field called Web Site to ensure its last four
characters are in an explicit set of valid website extensions.
Formula:
AND(
RIGHT( Web_Site__c, 4) <> ".COM",
RIGHT( Web_Site__c, 4) <> ".com",
RIGHT( Web_Site__c, 4) <> ".ORG",
RIGHT( Web_Site__c, 4) <> ".org",
RIGHT( Web_Site__c, 4) <> ".NET",
RIGHT( Web_Site__c, 4) <> ".net",
RIGHT( Web_Site__c, 6) <> ".CO.UK",
RIGHT( Web_Site__c, 6) <> ".co.uk"
)
Error Message: Web Site must have an extension of .com, .org, .net, or .co.uk.
35