How to Use Custom Data Validation Formula in Google Sheets

Data validation in Google Sheets is a very important skill. For example, you are making a sheet where users will insert business email addresses, but some people are writing without “@” or adding non-business emails.

If you apply the right data validation rule, Google Sheets will block that kind of input and give a warning message to the person who is doing the job.

In this article, we’ll talk about 5 widely used methods with specific use cases. Let’s explore them.

Key Takeaways

➤ Select the range D2:D13 where you want to apply data validation rules.
➤ Go to Data > Data validation (From the Menu Bar)
➤ Set criteria to “Custom formula is
➤ We want the exam score column to take data between 40 and 100 only.
➤ Enter this formula: =AND(ISNUMBER(D2), D2>=40, D2<=100)
➤ Now Column D won’t take any number that is not within this range.
➤ To give a warning, you can click on Advanced Options and write “Number should be between 40 and 100,” and users will see this error if they insert a number outside this range.
➤ Click on “Done.”
➤ You can see red marks at the top right corner of those numbers that are under 40.

overview image

Download Practice Workbook
1

Number Range Validation

In the following dataset, we have a list of students in Column A and their cities in Column B. In the third column (Column C), we’ve their Exam Score.

We want to make sure the values in the Exam Score column stay between 40 and 100. So, if anyone inserts data can’t add any data outside of this range.

Number Range Validation

Let’s begin.

➤ Select the Exam Score cells.  In this case, from C2 to C13.
➤ Go to the Data menu at the top and choose Data validation from the drop-down list. You will see the data validation option appear on the right side of your screen.

Number Range Validation

➤ Click on Add Rule. You will see some boxes appear. Don’t get afraid after seeing the bulk of options. Here is what you will do.

Number Range Validation

➤ In the Apply to Range box, write down the range only, nothing else. In our case, it is C2:C13.
➤ Then, click on the dropdown menu just under where you see Criteria. You should also click on the advanced menu so that we can show a warning sign.

Number Range Validation

➤ After clicking on the dropdown, you will see a bunch of lists. Scroll down and you will see an option “Custom formula is”. Select this one.

Number Range Validation

➤ Then, do these one by one. First, insert the formula = AND (ISNUMBER (C2), C2 >= 40, C2=100). This will ensure your data stays within this range. Feel free to change it if you want.

Number Range Validation

➤ After clicking the Advanced Options, you will see some new options.
➤ Make sure the Show help text for a selected cell is ticked.
➤ Write your help text in the box below. We wrote, “Please enter a value between 40 and 100.
➤ Select “Reject Input” from the “If the data is invalid:” option. And click on Done.

Now you will see a red sign at the top right corner of the data that does not meet our validation range. You will see the detailed message after hovering your mouse over the data. In our case, 101 and 25 are not within the range. Therefore, Google Sheets shows a red mark at the top-right corner, and when we hover over it, it displays the detailed message.

Number Range Validation

➤ Try to insert data that does not meet the range value and you will see the warning sign that you wrote.

Number Range Validation

That’s it. You have successfully completed the data validation method with a custom formula. Let’s try the next example, where we will find duplicate entries.

Note:
If you don’t select the “Reject input” option, Google Sheets will still accept invalid data and only show a small red mark in the corner. If the user misses it, he or she will continue inserting data that violates the validation. So, always check the Reject input radio button.


2

Duplicate Entry Validation

In the following dataset, we have a list of students in Column A and their cities in Column B. In the third column (Column C), we’ve added their Student ID.

Now, what we want is to make sure the values in the Student ID column are unique. That means each student should have a different ID. So, if someone tries to insert the same ID twice, Google Sheets will not accept it.

Let’s begin.

Duplicate Entry Validation

➤ Select the Student ID cells. In this case, from C2 to C13.

Duplicate Entry Validation

➤ Go to the Data menu at the top and choose Data validation from the drop-down list.

Duplicate Entry Validation

➤ You will see the data validation option appear on the right side of your screen.

Duplicate Entry Validation

➤ Click on Add Rule. You will see some boxes appear, just as you saw in the previous method.
➤ In the Apply to Range box, write down the range only, nothing else. In our case, it is C2:C13.

Duplicate Entry Validation

➤ Then, click on the dropdown menu just under where you see Criteria.
➤ After clicking on the dropdown, you will see a bunch of lists. Scroll down and you will see an option “Custom formula is”. Select this one.

➤ Then, do these one by one. First, insert the formula =COUNTIF($C$2:$C$13,C2)=1. This will ensure that every Student ID entered in Column C appears only once.

➤ After clicking the Advanced Options, you will see some new options.
➤ Make sure the Show help text for a selected cell is ticked.
➤ Write your help text in the box below. We wrote, “This Student ID already exists. Please enter a unique ID.”

Duplicate Entry Validation

➤ Select “Reject Input” from the “If the data is invalid:” option. And click on Done.

Duplicate Entry Validation

➤ Now try to enter a duplicate Student ID in Column C. If the ID already exists in the list and you will see the error message.

Duplicate Entry Validation

➤ And you will see red marks at the top right corner of all IDs that violate the rule, and now you can remove all the duplicate inputs.

That’s it. You have successfully completed the duplicate entry validation using a custom formula. Let’s go to the next one, where we will check for a length-based validation.


3

Length-Based Validation

In the following dataset, we have a list of students in Column A and their cities in Column B. In the third column (Column C), we’ve added their Student ID.

What we want is to make sure the Student ID has exactly five digits. No more and no less. If anyone tries to enter an ID with four or six digits, or something with letters, Google Sheets will reject that.

Length-Based Validation

➤ Select the Student ID cells. In this case, from C2 to C13.
➤ Go to the Data menu at the top and choose Data validation from the drop-down list.

Length-Based Validation

➤ You will see the data validation option appear on the right side of your screen. Click on Add Rule.

Length-Based Validation

➤ In the Apply to Range box, write down the range only, nothing else. In our case, it is C2:C13.
➤ Then, click on the dropdown menu just under where you see Criteria.

Length-Based Validation

➤ After clicking on the dropdown, you will see a bunch of lists. Scroll down and you will see an option “Custom formula is”. Select this one.

Length-Based Validation

➤ The data validation section will now look like this.

➤ Then, do these one by one. First, insert the formula =AND(ISNUMBER(C2), LEN(C2)=5). This will ensure that the Student ID has exactly five digits. You will immediately see a red mark at the top right corner of those ID numbers that violate the rule.

➤ Now, click on the Advanced Options, and you will see some new options.
➤ Make sure the Show help text for a selected cell is ticked.
➤ Write your help text in the box below. We wrote, “Please enter a five-digit numeric ID.
➤ Select “Reject Input” from the “If the data is invalid:” option. And click on Done.

Length-Based Validation

➤You will see a red mark at the top-right corner of the data that is not 5 digits. Hover over your muse, and you will see a more detailed message.

➤ Now try to enter an ID with fewer or more than five digits. Google Sheets will not accept the value, and a pop-up will appear with the warning sign you wrote in the help box.

Length-Based Validation

That’s it. Now let’s learn how to validate email in Google Sheets.


4

Domain-Specific Email Validation

In the following dataset, we have a list of students in Column A and their cities in Column B. In the third column (Column C), we’ve added their email addresses.

Now, what we want is to make sure all email addresses end with @sample.com. If someone enters Gmail, Yahoo, or anything else, Google Sheets will reject the data.

Domain-Specific Email Validation

➤ Select the Email cells. In this case, from C2 to C13.
➤ Go to the Data menu at the top and choose Data validation from the drop-down list.

Domain-Specific Email Validation

➤ You will see the data validation option appear on the right side of your screen.

Domain-Specific Email Validation

➤ Click on Add Rule. You will see some boxes appear.
➤ In the Apply to Range box, write down the range only, nothing else. In our case, it is C2:C13.

➤ Then, click on the dropdown menu just under where you see Criteria.
➤ After clicking on the dropdown, you will see a bunch of lists. Scroll down and you will see an option “Custom formula is”. Select this one.

Domain-Specific Email Validation

➤ The data validation section will now look like this.

Domain-Specific Email Validation

➤ Then, do these one by one. First, insert the formula =REGEXMATCH(C2, “@sample.com$”). This will make sure the email ends with @sample.com. You will immediately see a red mark at the top right corner of those emails that violate the rule.

➤ After clicking the Advanced Options, you will see some new options.
➤ Make sure the Show help text for a selected cell is ticked.
➤ Write your help text in the box below. We wrote, “Only @sample.com emails are allowed.”
➤ Select “Reject Input” from the “If the data is invalid:” option. And click on Done.

➤You will see a red mark at the top-right corner of the data that is not following the domain rule. Hover over your mouse, and you will see a more detailed message.

Domain-Specific Email Validation

➤ Now, try to enter any email that does not end in @sample.com. You will see the warning message that you entered earlier.

Domain-Specific Email Validation

That’s it. You have successfully completed the domain-specific email validation using a custom formula. Let’s go to the next one, where we will check for a specific input pattern using regex.


5

Pattern-Based Validation with Regex

In the following dataset, we have a list of students in Column A and their cities in Column B. In the third column (Column C), we’ve added their Student Code.

Now, what we want is to make sure all the codes follow the same pattern. Each code should be in the format of three uppercase letters, a hyphen, and then four digits. For example: ABC-1234. Any code that does not follow this pattern, Google Sheets will reject those.

Pattern-Based Validation with Regex

➤ Select the Student Code cells. In this case, from C2 to C13.
➤ Go to the Data menu at the top and choose Data validation from the drop-down list.

Pattern-Based Validation with Regex

➤ You will see the data validation option appear on the right side of your screen.

Pattern-Based Validation with Regex

➤ In the Apply to Range box, write down the range only, nothing else. In our case, it is C2:C13.
➤ Then, click on the dropdown menu just under where you see Criteria.

Pattern-Based Validation with Regex

➤ After clicking on the dropdown, you will see a bunch of lists. Scroll down and you will see an option “Custom formula is”. Select this one.

Pattern-Based Validation with Regex

➤ The Data Validation section will look like this now.

Pattern-Based Validation with Regex

➤ Then, do these one by one. First, insert the formula =REGEXMATCH(C2, “^[A-Z]{3}-\d{4}$”). This will make sure the input follows the exact format: three capital letters, a dash, and four numbers.
➤ You will immediately see a red mark at the top right corner of those codes that violate the rule.

Pattern-Based Validation with Regex

➤ After clicking the Advanced Options, you will see some new options.
➤ Make sure the Show help text for a selected cell is ticked.
➤ Write your help text in the box below. We wrote, “Enter a code in the format ABC-1234.

➤You will see a red mark at the top-right corner of the data that is not following the code rule. Hover over your mouse, and you will see a more detailed message.

➤ Now try to enter a code like abc-1234, XYZ_0000, or 123-ABCD. Google Sheets will reject the input, and you will see the warning message that you entered earlier.

That’s it. You have successfully completed the pattern-based validation using a custom formula


Frequently Asked Questions

What types of functions are supported in custom formula validation?

You can use any function that returns TRUE or FALSE. You can use Functions like ISNUMBER, LEN, COUNTIF, REGEXMATCH, AND, and OR. If your formula doesn’t return TRUE or FALSE, the validation won’t work.

Can I use FILTER or VLOOKUP directly in custom formula validation?

No, you can’t use those functions directly in the data validation formula box. Google Sheets does not support that. If you really need them, you can use a helper column instead.

Why does Google Sheets reject my custom formula rule without error?

This usually happens when your formula doesn’t match your selected range. For example, if you start from C2, your formula should also refer to C2, not any other cell. You should also make sure your formula returns True or False.

Is there a limit to how many rows a custom formula validation can cover?

No. There is no official limit. But if you apply a custom formula to a huge range (let’s say over thousands), then it can make the process slow; in the worst case, the browser can be non-responsive.

Can I use REGEXMATCH in both data validation and conditional formatting?

Yes, you can and you should do that. It works better for checking emails, codes, or a specific keyword.


Concluding Words

In this article, you learned how to apply data validation in Google Sheets using custom formulas. We have shown you 5 different methods and examples. We showed with use cases so you can learn where you should use a custom formula.

We have added the worksheet. You can download the worksheet for practice purposes. Also, if you have any specific problems or questions, please ask us in the comment box below.

ExcelInsider Team
We will be happy to hear your thoughts

Leave a reply

Excel Insider
Logo