How to validate an operator entry against a set format using regex
Use a regex validation rule to check that what an operator types matches your required format, such as a batch code, and mark the field invalid when it does not.
What you'll build
- A validation rule that marks a text field valid when the entry matches your format.
- A fall-back rule that turns the field red whenever the format is not met.
Step 1 — Write down the format you need
OFS-Flow includes built-in validation patterns for times, dates, integers and decimals. Regex is for the formats those do not cover, such as batch codes, chemical test results or your own date codes.
Take a batch code printed on pack:
BAT-25M-3JD
Break it into its parts and describe the pattern of each, not the specific characters:
| Section | What it is | Regex | Meaning |
|---|---|---|---|
| BAT | Product reference, changes with each SKU, letters only | [A-Z]{3} |
Three consecutive uppercase letters |
| 25M | Two-digit week number, then a letter for the production year | \d{2}[A-Z] |
Two digits followed by one uppercase letter |
| 3JD | Workcentre and shift reference, letters or numbers | [A-Z0-9]{3} |
Three consecutive letters or digits |
Step 2 — Join the parts into one pattern
Combine the three components, keeping the dashes that sit between them, then wrap the whole thing in ^ and $:
^[A-Z]{3}-\d{2}[A-Z]-[A-Z0-9]{3}$
^ anchors the pattern to the start of the entry and $ anchors it to the end. Without them, an operator could type extra characters before or after a valid code and the field would still pass.
Step 3 — Add the validation rule to the field
Open the form, go to the Form tab, and select the text field you want to validate. Open Field Validation Rules.
Add a rule that reads: mark form as Valid without message, when the field match regex your pattern. Paste the pattern into the value box.

Step 4 — Add the fall-back rule
Regex in OFS cannot be negated, so there is no way to write a rule for "does not match". Instead, add a second rule below the first:
- Otherwise, mark form as Invalid without message, when
1=1.
1 = 1 is always true. The second rule is only reached when the first rule is not satisfied, so the effect is that the field turns red and blocks submission whenever the entry fails the pattern. Rule order matters here, so keep the regex rule above the fall-back.
Test it
Save the form and try each of these in the field:
BAT-25M-3JDvalid.bat-25M-3JDinvalid, the letters are lowercase.BAT-25M-3JD1invalid, there is an extra character after the pattern.BAT-5M-3JDinvalid, the week number is one digit.
If everything comes back valid, check that ^ and $ are both present in your pattern.
Adapting it to your own codes
- Change the counts in the braces to match your format:
{4}for four characters,{2,4}for between two and four. - Use
\dfor any digit,[A-Z]for any uppercase letter,[A-Z0-9]for either. - If your code allows lowercase, use
[A-Za-z]rather than[A-Z]. - Keep the separators exactly as they appear on pack. A pattern built with dashes will reject an entry typed with spaces or full stops.
- Add a message to the invalid rule if operators need to be told what the correct format looks like.
Regex Cheat Sheet
.png?width=670&height=1029&name=ofs-regex-cheatsheet%20(1).png)