I worked as a Salesforce admin for an organization that uses Salesforce CRM, and our sales team created opportunity records for converted leads.
While creating opportunity records, they had to restrict the close date field so that it would not be less than today’s date. Whenever the sales rep enters the close date today or earlier, an error message should display, and the record should not get saved.
To fulfill that requirement, we have a validation rule in Salesforce. Here, I will explain the validation rules in Salesforce, and then I will show you how to create a validation rule and apply it to different fields in Salesforce.
Validation Rule in Salesforce
In Salesforce, the validation rule requires the user to enter the correct data to save the records. If the user enters invalid input based on certain criteria, an error message will display on the screen.
Using the validation rule successfully gives quality data. The validation rule is evaluated regardless of the medium from which you create the records.
Below are some examples of applying validation rules on fields in Salesforce.
Validation Rule on Text Field in Salesforce
Here, we will take the Text Field example to display an error message when the text field contains a Blank Value or a Numeric Value.
- Go to the Gear Icon. -> Click on the Setup. -> Select the Object Manager tab.

- Select the Object on which you want to create the Validation Rule. Here, I’m going to create an Account Object.

- Now select the Validation Rules from the Account object.

- Now select the New button to create the new validation rule.

- Now, enter the Rule Name and check the Active checkbox to apply the rule. Then, enter the logic for Error Conditions in the Formula Editor.
ISBLANK( Site ) ||
NOT(REGEX( Site, "^[^0-9]*$"))- ISBLANK(): This function checks if the field is blank.
- NOT(REGEX( Site, “^[^0-9]*$”)): This regular expression checks that the field does not contain a numeric value.
- Site: This is the Account Field; we can insert it from the Insert Field button.
Now, to check whether the entered code is correct or not, click on the Check Syntax button.

- Error Message: Now, enter a message to display an error when the user enters incorrect details. Error Location: Where the error message should display on the screen. For that, we have the following two options.
- Top of Page: The error message will display beside the Save button.
- Field: The error message will display on a particular field for which we apply validation.
- Then click on the Save button.

- Now, navigate to the Account Record Detail Page to create a record and enter the incorrect detail in the field where we created the validation rule.
- Here, you can see that the validation rule has been successfully applied, and an error message is displayed.

In this way, we can add a validation rule for the text field in Salesforce.
Validation Rule on Number Field in Salesforce
Here, we will take the Number Field example to display an error message when the user enters out-of-range numbers. For example, we have the Rating_0_5__c field on an Account, which ranges from 0 to 5.
- Follow the above step to navigate to the Object and Validation Rule option. Here also, I’m creating a rule on the Account object.
- Now, enter the Rule Name and check the Active checkbox to apply the rule. Then, enter the logic for Error Conditions in the Formula Editor.
OR( Ratings_0_5__c < 0, Ratings_0_5__c > 5)- This formula will display an error if the user does not enter a value in the Number Field within the specified range (0-5).

- Now enter the Error Message and the Error Location. Here, I have selected the ‘ Field as the error location of the ‘Rating 0-5’ Field. Then click on the Save validation rule.

- Here, you can see that if the user enters a number out of range, it displays an error message.

This is how to add a validation rule for the Number Field in Salesforce.
Validation Rule on Date Field in Salesforce
Here, we will take the Date Field example to display an error message when the user selects the date field Today or Less than Today.
- Follow the above step to navigate to the Object and Validation Rule option. I’m going to create a validation rule for the Close Date of Opportunity object.
- Now, enter the Rule Name and check the Active checkbox to apply the rule. Then, enter the logic for Error Conditions in the Formula Editor.
CloseDate <= TODAY()- Today(): This function returns the Current Date.
- CloseDate: This is the Opportunity Field; we can insert it from the Insert Field button.
This formula will display an error when the user selects a close date that is today or earlier.

- Now, enter the Error Message that will display on the Record Detail Page and select the error message location. Here, I have selected the error location in the Close Date Field.
- Then click on the Save button.

- Today’s date is 20/05/2024. However, when the user selects a Close Date less than Today, an error message is displayed in the close date field.

This is how to add a validation rule for the Date Field in Salesforce.
Conclusion
I hope you now have an idea about validation rules in Salesforce. We have seen what validation is in Salesforce and how to create and apply validation rules to different Fields in Salesforce.
I explained the validation rule by applying it to the text, number, and date fields. So, using a validation rule, we can restrict the user from entering incorrect data in Salesforce.
You may like to read:
- Validation Rule for Phone Number in Salesforce
- Entitlement Rules in Salesforce
- Salesforce Checkbox Validation Rule
- How to Use AND operator in Validation Rule in Salesforce
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.