0

I have a number field Qualification_Year__c; I want to ensure that users should only enter year format in this field example 2010, 2015 etc. How can I write a Validation rule using regex to enforce this?

1
  • You should edit your question to show us what you've tried so far. trailhead.salesforce.com is the place to start learning if you're unfamiliar with a concept/feature of Salesforce. Downvoted because this is a "do my work for me" question.
    – Derek F
    Apr 29, 2021 at 11:17

1 Answer 1

1

If it's a number field, the validation rule can be:

Field < 1000 || Field > 9999

This also gives you the ability to fine tune it to say, just a certain range of years.


If it's a text field:

NOT(REGEX(Field, "^\\d{4}$"))

Which is pretty literal. The regex matches any four digits (but nothing else), and since rules fire when true, it is then flipped about by NOT, so it fires when it does not match.


Another common strategy is to just use a picklist if there's only a range of years you want to allow. No validation rule needed, since the UI forces them to pick a value from a list you specify.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .