Sub topic 1
What data validation does and how to open it
Data validation in Google Sheets is a rule attached to a cell or range. The rule decides which values the cell accepts. Instead of cleaning up inconsistent entries afterward, you stop most of them at the moment someone types them. This chapter explains what a rule is made of, how Sheets applies it, the ways to open the rule editor, and how to read a cell's rule once it exists.
Prerequisites
This chapter assumes you can already do the following:
- Select a cell, a range such as
A2:A100, and a whole column. - Type values and basic formulas into cells.
- Tell relative references (
A2) from absolute ones ($A$2). - Refer to a cell on another sheet, for example
Lists!A2.
You do not need any scripting experience. The Apps Script snippet near the end is optional and only reads a rule.
What a validation rule is
A validation rule is metadata stored on cells, separate from their values. A cell can hold the value 42 and also carry a rule that says "numbers between 1 and 10 only". Both facts are stored independently, which explains several behaviors later in this tutorial.
Every rule has the same parts:
| Part | What it controls | Example |
|---|---|---|
| Apply to range | Which cells the rule covers | `C2:C100` |
| Criteria | What counts as a valid value | Dropdown, Number, Date, Custom formula |
| Criteria values | The specifics of the criteria | The items `Not started, In progress, Done` |
| Invalid-data behavior | What happens when a value fails | Show a warning, or reject the input |
| Help text (optional) | A message shown to the person editing | "Pick a status from the list." |
| Appearance (dropdowns only) | How the selected item is displayed | Chip, plain arrow, or plain text |
Later chapters cover each part in detail. For now, remember that a rule always answers two questions: which cells, and what is allowed in them.
What validation does and does not do
Validation runs when a person edits a cell by typing, selecting from a drop-down, or ticking a checkbox. If the new value passes the criteria, Sheets accepts it silently. If it fails, Sheets either rejects the edit or accepts it and marks the cell, depending on the rule's invalid-data setting.
The following diagram shows that decision.
Validation is a guard for typed input, not a security feature. Three behaviors catch people out:
- Existing values are not checked when you create a rule. If
C2:C100already holdsFinished, and you then add a rule that allows onlyDone, Sheets keepsFinished. It does flag the cell with a small red triangle in the corner, and hovering over it shows the validation message. It does not change the value. - Pasting can bypass or replace rules. A normal paste copies the source cell's rule along with its value, so it overwrites the rule on the target. Paste special with "values only" skips the check. The chapter on validation limits covers this.
- Formulas are not validated. A rule checks what a person enters. If a cell contains a formula, the result is not tested against the rule.
How to open data validation
There are several routes to the same rule editor. Pick the one that fits what you are doing.
From the Data menu
This is the most general route and the one this tutorial uses.
- Select the cells you want to restrict, for example
C2:C100. - Choose Data > Data validation.
- The Data validation rules side panel opens on the right.
- Click Add rule.
If the selected cells already carry a rule, the panel lists it. Click it to edit it. If you selected cells with no rule, the panel shows an empty list and the Add rule button.
From the right-click menu
Right-click a cell and open the menu. Depending on your Sheets version, the validation entry is either listed directly or under View more cell actions. In both places you get the same side panel. This route is quick when you are already working on a cell.
From the Insert menu
Insert > Dropdown creates a drop-down rule on the selected cells and opens the same side panel with the criteria already set to a drop-down. Insert > Checkbox applies a checkbox rule immediately, without opening the panel. These are shortcuts for the two most common rule types, and the result is a normal validation rule that you can edit later.
From the smart-chip shortcut
Typing @ in a cell opens a menu of building blocks. Choosing Dropdown from it creates a drop-down rule too. This also produces an ordinary rule, so everything in this tutorial applies to it.
From the menu search
If you cannot find an item, press Alt + / (on Mac, Option + /) and type "data validation". Sheets searches its menus and runs the matching command.
The rule editor
In the side panel, a rule has these controls, from top to bottom.
- Apply to range. A text box showing the range, for example
C2:C100. You can edit it by hand, or click the small grid icon to pick a range with the mouse. You can list more than one range, separated by commas. - Criteria. A menu with the rule types. The current menu offers these:
| Criteria | Allows |
|---|---|
| Dropdown | One or more items from a list you type |
| Dropdown (from a range) | Items read from cells, including cells on another sheet |
| Number | Numbers compared against limits, such as between 1 and 10 |
| Text | Text that contains, does not contain, equals, or looks like an email or URL |
| Date | A valid date, or a date before, after, or between limits |
| Custom formula | Anything a formula that returns TRUE or FALSE can express |
| Checkbox | A checked or unchecked state, or custom values for each |
- Advanced options. A collapsed section holding the invalid-data behavior, the help text, and, for drop-downs, how the selected item looks.
- Done. Saves the rule. If you close the panel without clicking Done, the rule is not saved.
Clicking Add another rule adds a second rule to the same cells or to different ones. The panel shows all rules on the sheet as a list, each with its range and a short description of its criteria.
A first rule, step by step
This walk-through creates a rule that lets a quantity column accept only whole numbers from 1 to 100.
- In a blank sheet, type
QuantityinB1. - Select
B2:B50. - Choose Data > Data validation, then Add rule.
- Confirm that Apply to range shows
B2:B50. - In Criteria, choose Number, then between, and enter
1and100. - Open Advanced options and choose Show a warning for invalid data. Leave the other options alone.
- Click Done.
Now test it. Type 25 in B2. It is accepted. Type 250 in B3. Sheets accepts the value but marks the cell with a red triangle, and hovering shows a message that the input falls outside the allowed numbers. Switch the rule to Reject input and try 250 again. The edit is refused and Sheets shows an error. The next chapters build on the difference between those two settings.
One detail to check: Number between 1 and 100 still allows 2.5. To require whole numbers, you would need a custom formula such as =ISINTEGER(B2), which a later chapter covers. Always test boundary and odd values, not just obvious ones.
Reading the rule on an existing cell
In the interface, click a cell and open Data > Data validation. The panel highlights the rule that covers the cell. This is the quickest way to find out why a sheet someone else built refuses your input.
You can also read it with Apps Script. Open Extensions > Apps Script, paste the function below, and run it after selecting the sheet that holds your rule.
function describeValidation() {
const cell = SpreadsheetApp.getActiveSheet().getRange('B2');
const rule = cell.getDataValidation();
if (rule === null) {
Logger.log('B2 has no validation rule.');
return;
}
Logger.log('Criteria type: ' + rule.getCriteriaType());
Logger.log('Criteria values: ' + JSON.stringify(rule.getCriteriaValues()));
Logger.log('Allows invalid input: ' + rule.getAllowInvalid());
Logger.log('Help text: ' + rule.getHelpText());
}getDataValidation() returns null for a cell without a rule, so the check for null matters. getAllowInvalid() returns true for the warning behavior and false for reject. For the quantity rule above, the log shows the criteria type NUMBER_BETWEEN and the values [1,100]. The first time you run it, Google asks you to authorize the script.
Common errors when starting out
- Rule applied to the wrong range. If the header row is included,
B1is validated too. Check Apply to range before clicking Done. - Panel closed without Done. The rule silently does not exist. Reopen the panel and look for it in the list.
- Expecting existing data to be fixed. Rules do not rewrite old values. Look for red triangles and correct them by hand or with a formula.
- Expecting a rule to survive a paste. Pasting a cell from elsewhere replaces its rule with the source cell's rule, or removes it if the source has none.
- Testing only by pasting. Pasted values may skip the check, so a rule can look broken or work by accident. Test by typing.
Summary
A validation rule attaches a range, a criterion, and an invalid-data behavior to cells. Sheets checks it when someone types or selects a value. You open it from Data > Data validation, from the right-click menu, or from Insert > Dropdown and Insert > Checkbox. Rules guide input but do not guard against pasting, formulas, or existing values.
Exercise
You select C2:C100 and add a drop-down rule that allows only "Done". Column C already contains the value "Finished" in C5. What happens to C5, and how can you find out which cells now break the rule?
Show answer
C5 keeps the value "Finished", because a new rule does not change existing data. Sheets marks the cell with a small red triangle in the corner, and hovering over it shows the validation message. Scan the range for red triangles to find all cells that break the rule, then correct them by hand.
Exercise
A colleague says "I set a rule on B2:B50, but I can still get 250 into B10." Name two likely reasons, other than the rule being absent.
Show answer
The invalid-data setting may be "Show a warning", which accepts the value and only marks the cell. Alternatively, the value was pasted: a normal paste replaces the target cell's rule with the source cell's rule, and paste special with values only skips the check. A formula that returns 250 would also pass, since results of formulas are not validated.
Exercise
Write the Apps Script call that returns the validation rule on cell D4, and say what it returns if D4 has no rule.
Show answer
SpreadsheetApp.getActiveSheet().getRange('D4').getDataValidation() returns the rule. If D4 has no rule it returns null.