New Tool Radar

AI for Data & Spreadsheets

How to Enforce an ID Format With a Custom Excel Data Validation Formula

Write a custom data validation formula that only accepts IDs with the right prefix and length, such as INV- plus four digits.

Schematic with three steps: Write the format formula for the first; Set an error message that states the; Test valid and invalid IDs.

IDs only work as lookup keys when they all follow the same shape. A custom validation formula can require the right prefix, the right length and digits only, so a mistyped ID is caught at entry instead of failing a lookup later.

What you need

  • Excel with the ID column ready
  • The exact ID pattern: prefix, length and character rules
  • A few sample IDs, valid and invalid, to test with

Step 1: Write the format formula for the first cell

Select the ID cells, open Data, Data Tools, Data Validation, choose Allow: Custom, and write a formula that returns TRUE only for the right shape, for example =AND(LEN(A2)=8,LEFT(A2,4)=’INV-‘,ISNUMBER(–RIGHT(A2,4))) for INV- plus four digits. Test it in a helper cell first.

Step 2: Set an error message that states the pattern

On the Error Alert tab, write a message that shows the pattern, such as Enter an ID like INV-2041. A message that only says the value is invalid does not tell the typist what to fix.

Step 3: Test valid and invalid IDs

Type a correct ID, one with the wrong prefix, one too short, and one with a letter where a digit belongs. Only the correct ID must be accepted.

Worked example: synthetic IDs in the shape INV- plus four digits

The table below is a synthetic example. It shows the expected result of testing an ID format rule; no workbook was opened.

ID tested Matches the pattern? Result
INV-2041 Yes Accepted
INV-241 No – too short Blocked
2041 No – no prefix Blocked
INV-2041A No – too long Blocked

Expected result (UNTESTED). only INV-2041 is accepted; the short, prefix-free and over-long IDs are all blocked. The format rule works as intended. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • Text length alone does not check content; a five-character rule accepts ABCDE in a ZIP column, so use a custom formula for format.
  • The formula is written relative to the active cell, so select the range with the right active cell first.
  • If the formula itself errors, the entry is rejected, which can look like a rule that rejects everything.

Sources

See all