Salesforce Validation Rule Examples: 15 Formulas You Can Copy (with Practice Questions)

Header image: Salesforce validation rule examples, showing an error condition formula that blocks Closed Lost opportunities without a loss reason.

Hey friends,

A validation rule blocks a save when its formula returns TRUE, so you write the formula to describe the bad data, not the good data. Below are 15 examples you can copy, a few straight from Trailhead and the rest the kind I've built in real orgs, each with a one-line explanation. If formula syntax is new to you, start with my Salesforce formula examples.

Validation rules sit in the Admin exam's Data and Analytics section, which also asks you to compare them with duplicate and matching rules. The basics here come from Trailhead's Validation Rules unit.

Validation rules fire when the formula returns true: true means invalid data and the save is blocked with an error, false means the record saves.

If you remember one thing, remember this.

15 validation rule examples

Replace the custom field names (ending in __c) with your own. Test every rule in a sandbox first.

1. Account number must be numeric if entered

Validation rule
AND( NOT(ISBLANK(AccountNumber)), NOT(ISNUMBER(AccountNumber)) )

From Trailhead. Blank is allowed; anything typed must be a number.

2. Date must be in the current year

Validation rule
YEAR( My_Date__c ) <> YEAR( TODAY() )

From Trailhead. Swap in your own date field. For more date functions, see my Salesforce date formulas.

3. Salary range no wider than $20,000

Validation rule
(Salary_Max__c - Salary_Min__c) > 20000

From Trailhead's salary range example.

4. Closed Lost needs a loss reason

Validation rule
AND( ISPICKVAL(StageName, 'Closed Lost'), ISBLANK(Loss_Reason__c) )

Use ISPICKVAL for picklists; you can't compare them with = directly.

5. Close date can't be in the past on open deals

Validation rule
AND( NOT(IsClosed), CloseDate < TODAY() )

Stops stale pipeline.

6. Discount over 20% needs a reason

Validation rule
AND( Discount__c > 0.2, ISBLANK(Discount_Reason__c) )

Percent fields are stored as decimals, so 20% is 0.2.

7. Phone must have 10 digits (US)

Validation rule
AND( NOT(ISBLANK(Phone)), LEN( SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(Phone, '(', ''), ')', ''), '-', ''), ' ', '') ) <> 10 )

Strips common formatting before counting.

8. Website must end in .com, .org or .net

Validation rule
AND( RIGHT(Web_Site__c, 4) <> '.com', RIGHT(Web_Site__c, 4) <> '.org', RIGHT(Web_Site__c, 4) <> '.net' )

Simplified from Trailhead's example, which also checks uppercase.

Cert Lab by Nick Frates

Practice validation rule questions free

The Admin Cert Lab's Data and Analytics lessons cover validation, duplicate and matching rules, with practice questions that explain every option.

Start the free Admin Cert Lab →

9. End date after start date

Validation rule
End_Date__c < Start_Date__c

The simplest useful rule there is.

10. Can't change Amount once Closed Won

Validation rule
AND( ISCHANGED(Amount), ISPICKVAL(PRIORVALUE(StageName), 'Closed Won') )

ISCHANGED and PRIORVALUE only work on edits.

11. Stage can't go backward from Closed Won

Validation rule
AND( ISCHANGED(StageName), ISPICKVAL(PRIORVALUE(StageName), 'Closed Won') )

Locks the stage after the deal closes.

12. Billing country must be two letters

Validation rule
AND( NOT(ISBLANK(BillingCountry)), LEN(BillingCountry) <> 2 )

Trailhead has a fuller version that checks real ISO codes.

13. Email required for Web leads

Validation rule
AND( ISPICKVAL(LeadSource, 'Web'), ISBLANK(Email) )

Conditional requirement without a page layout change.

14. Only managers can approve

Validation rule
AND( ISCHANGED(Approved__c), Approved__c, $Profile.Name <> 'Sales Manager' )

$Profile and $User let rules depend on who's saving. Better: check a custom permission.

15. Contact zip must match account zip

Validation rule
AND( NOT(ISBLANK(AccountId)), MailingPostalCode <> Account.ShippingPostalCode )

This is Trailhead's hands-on challenge. Contacts without an account are allowed.

Validation rule vs duplicate rule vs Flow

  • Validation rule: this record's field values are wrong. Block the save.
  • Duplicate and matching rules: this record already exists. Warn or block.
  • Flow: fix or derive the value automatically instead of blocking the user. And you can bypass validation rules in Flow when automation needs to.

Tips

Practice questions

1. A rule should require Email only when Lead Source is Web. Which formula is right?

  • A.
    ISPICKVAL(LeadSource, 'Web') || ISBLANK(Email)
  • B.
    AND(ISPICKVAL(LeadSource, 'Web'), ISBLANK(Email))
  • C.
    AND(LeadSource = 'Web', NOT(ISBLANK(Email)))

Answer: B. A blocks every blank email. C compares a picklist with = and blocks the good data. For when validation rules run relative to triggers and flows, see the Salesforce order of execution.

2. Users must not change Amount after a deal is Closed Won, but new deals must save normally. Which functions do you need?

Answer: ISCHANGED and PRIORVALUE, as in example 10.

Keep studying

Hope this helps!

Best,

Nick

Keep reading

See all posts →

Salesforce Saturdays

Join 1,032 Salesforce pros getting one 10-minute tip on Salesforce tech or career growth every Saturday.