Excel Text Length Data Validation: Step-by-Step
In this article
Yes, Excel can enforce a maximum (or exact) text length in any cell using Data Validation. You select the cells, open the Data Validation dialog, choose the "Text length" rule, set your limit, and Excel will then reject any entry that breaks it. It works in Excel for Windows, Excel for Mac, and the web version, and it takes about a minute to set up. This is the standard way to keep things like IDs, phone numbers, postcodes, or username fields to a fixed length in a spreadsheet others will fill in.
What You'll Need
- Excel (desktop app for Windows or Mac, or Excel for the web)
- A worksheet with the cells you want to restrict
- The length rule you want to enforce (for example, exactly 8 characters, or between 2 and 20)
Step-by-Step
- Select the cell or range you want to validate. To apply it to a whole column that people will fill in later, select a generous range now, for example A2:A500, rather than the entire column.
- Go to the Data tab on the ribbon and click Data Validation. Exact button placement can differ slightly between Excel versions and platforms, but it is always on the Data tab; in Excel for the web it may appear under a small menu.
- In the dialog that opens, on the Settings tab, click the Allow dropdown and choose Text length.
- Choose the condition under Data: "between", "not between", "equal to", "not equal to", "greater than", "less than", and so on. For a maximum length, pick "less than or equal to"; for an exact length, pick "equal to".
- Enter the number(s). For "between", type the minimum in the first box and the maximum in the second. For "equal to", type the single required length.
- Leave "Ignore blank" ticked if empty cells should be allowed, which is usually what you want. Untick it only if every cell must be filled.
- Click the Input Message tab if you'd like a hint that appears when the cell is selected. Tick "Show input message after cell is selected", then type a short title and message such as "Enter exactly 8 characters".
- Click the Error Alert tab. Make sure "Show error alert after invalid data is entered" is ticked. Pick a style: Stop blocks the entry completely, Warning lets the user override, and Information just informs. Type a clear message like "This field must be 8 characters exactly."
- Click OK. Test it: type an entry that is too long or too short into one of the cells and confirm you see your error message.
Common Mistakes
- Applying the rule only to cells that already contain data. Validation does not apply to cells added outside the original range, so plan the range for future entries.
- Using "Warning" or "Information" style when you actually need a hard block. Only the "Stop" style truly prevents invalid entries.
- Assuming validation checks data that is pasted in. Copy-paste can bypass data validation entirely, so entries pasted in bulk are not checked.
- Confusing character count with number size. Text length counts every character including spaces, so "AB 12" is 5 characters, not 4.
- Forgetting that validation is not retroactive. Data already in the cells before you set the rule is not removed or flagged automatically.
- Selecting the whole column (millions of cells) when only a few hundred are needed, which can slow the file down and make Circle Invalid Data very slow.
Tips
- To flag existing bad data, use Data > Data Validation > Circle Invalid Data. Excel draws red circles around cells that break the rule.
- To copy a validation rule to other cells, copy a validated cell, then use Paste Special and pick Validation.
- To edit or remove a rule, select the cells, reopen Data Validation, and change the settings or click "Clear All".
- Combine a text length rule with number formatting for things like account numbers that must not lose leading zeros: format the cells as Text before entry.
- If you need more complex logic than the built-in options, choose "Custom" in the Allow list and write a formula, for example
=AND(LEN(A2)=8,ISNUMBER(A2)).
Troubleshooting
- Invalid entries are still accepted. Reopen Data Validation for the cell and check the Error Alert tab: "Show error alert" must be ticked and the style set to Stop. Also confirm the cell is actually inside the validated range.
- Pasted data broke the rule. Paste operations skip validation. Remove the pasted values, use Circle Invalid Data to find offenders, or re-enter them manually. You can also protect the sheet so only manual entry in the allowed cells is possible.
- The Data Validation button is greyed out. The sheet is probably protected. Unprotect it (Review > Unprotect Sheet, with the password if one was set), then add the rule.
- The rule disappeared on new rows. You selected too small a range. Select a larger range and reapply, or format your data as a Table (Ctrl+T), which can extend calculated-column and validation behaviour as rows are added.
- Numbers are rejected unexpectedly. Text length counts digits too, so a 10-digit number is length 10. If the field should accept either, use a Custom formula instead of Text length.
Frequently Asked Questions
Does data validation work in Excel for the web?
Yes. You can create and edit Text length validation rules in Excel for the web from the Data tab, and existing rules in desktop-created files are enforced there too.
Does text length count spaces?
Yes. LEN counts every character, including spaces, punctuation, and leading or trailing spaces. Use TRIM in a Custom formula if you want to ignore extra spaces.
Can I enforce a minimum and maximum at the same time?
Yes. Choose "between" as the Data condition and enter the minimum and maximum, for example between 2 and 20 characters.
Will validation stop someone from pasting invalid data?
No. Pasting bypasses data validation. Use Circle Invalid Data afterwards to find problems, or lock the sheet so only the intended cells accept typing.
Is there a limit to how many characters a cell can hold?
Excel itself has a hard cell limit of 32,767 characters, and 255 characters visible in a cell under some conditions. Your validation rule operates well below these, so they rarely matter in practice.
Can I validate text length in Google Sheets the same way?
Not with the same menu. Google Sheets has no built-in Text length option; you use Data validation with a custom formula such as =LEN(A2)<=20 under "Reject input".