Regex Validation
A Regex validation checks the format of the value entered in a Form Field against a pattern known as a regular expression (or regex). This is useful for enforcing standard formats such as email addresses, phone numbers, or reference numbers.
When to use it
Use Regex validation to make sure the data end users enter follows a predictable format, for example:
- An email address (for example
john.smith@company.com) - A phone number (for example
+27 82 123 4567) - An ID or reference number (for example
PF-2026-000123) - A postal code
How to configure it
The Regex validation is configured on the Form Field's Validations tab in the field properties drawer. Refer to Manage Fields for how to open the drawer.
- In the
Validationstab, turn on theRegextoggle. - Under Configure Regex using, choose Fixed Value and enter the regular expression in the Regex Validator text box.
- Choose a Regex Behaviour from the drop-down (see below).
- Enter a message in the Regex Validation Error Message text box. This message is shown to the end user whenever the value does not match the pattern.
Use a pattern from a Reference Data Query
Choose this option when the required format depends on another answer, such as the type of identity document selected by the user. Each matching reference data record must contain the regex pattern for that document type.
- Configure a Reference Data Query on the form that uses the other answer (for example, ID type) to find the appropriate record. Include the attribute containing the pattern in the query's Additional Stored Attributes.
- In the field's
Validationstab, turn onRegexand select Reference Data Query Attribute under Configure Regex using. - Select the Reference Data Query, then select its Attribute containing the pattern. The Attribute list shows the query's additional stored attributes.
- Choose a Regex Behaviour. For Show Error Message, enter the message to display when the value does not match the returned pattern.
- Save the field and publish the form. Check each expected ID type in the form tester with a matching and a nonmatching value.
For example, a query filtered by ID type could return ^[A-Z]{2}[0-9]{6}$ for one type and ^P[0-9]{8}$ for another. The returned pattern must start with ^ and end with $. Reference data patterns support letters, digits, spaces, hyphens, slashes, character ranges such as [A-Z] and [0-9], escaped punctuation, and repetitions such as {6}, +, *, and ?. A numbered repetition may not exceed 64. Groups, alternatives (|), lookarounds, and backreferences are not supported for this option. These limits apply only to patterns returned by a Reference Data Query; Fixed Value patterns retain their existing syntax.
If the query does not return a record, its selected attribute is empty, or the pattern is unsupported, the form shows “The validation format is unavailable. Please try again.” and does not accept the value. Check that the selected ID type matches a query record and that its pattern attribute contains a supported expression.
Regex Behaviour
| Behaviour | Description |
|---|---|
| Show Error Message | The value the user entered is kept as-is and the error message is displayed when it does not match the pattern. |
| Trim characters based on Regex | The value is automatically cleaned up so that it matches the pattern. Leading and trailing spaces are removed, and any characters that are not allowed by the pattern are removed as the user types. The (optional) error message is not shown because the value is corrected instead. |
Error message placeholders
The error message can include the special placeholder {0}, which is replaced with the field's label when the message is displayed. For example, an error message of "{0} must be a valid email address" is shown as "Email must be a valid email address".
Writing a regular expression
A regular expression is written using special characters that describe the format the value must follow. Some of the most common building blocks are:
| Pattern | Meaning | Example |
|---|---|---|
^ |
Start of the value | ^A matches only values that start with an A |
$ |
End of the value | A$ matches only values that end with an A |
[a-z] |
Any single lower-case letter | [a-z] matches a, b, ... z |
[0-9] |
Any single digit | [0-9] matches 0, 1, ... 9 |
\d |
Any single digit (same as [0-9]) |
\d matches a digit |
\s |
Any whitespace character (space, tab) | \s matches a space |
. |
Any single character | . matches any letter, digit, or symbol |
+ |
One or more of the previous item | \d+ matches one or more digits |
* |
Zero or more of the previous item | a* matches "", a, aa, ... |
{2,4} |
Between 2 and 4 of the previous item | \d{2,4} matches 2, 3, or 4 digits |
\| |
Or | red\|blue matches red or blue |
\ |
Escape the next character (match it literally) | \. matches a literal full stop |
Best practice: anchor your pattern
Use ^ at the start and $ at the end of the pattern to require the entire value to match. Without the anchors, the pattern only needs to be found somewhere in the value.
^[0-9]{5}$— the whole value must be exactly 5 digits (12345✓,123✗,123456✗,ab123✗)[0-9]{5}— at least 5 digits appear somewhere (123✗,ab12345✓)
Example regexes
| Purpose | Regex | Matches | Does not match |
|---|---|---|---|
| Email address | ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ |
john.smith@company.com |
john.smith@company |
| South African phone number | ^(\+27\|0)[0-9]{9}$ |
+27821234567, 0821234567 |
27821234567, 082 123 4567 |
| Alphanumeric reference | ^[A-Z]{2}-[0-9]{6}$ |
PF-123456 |
PF-12345, pf-123456 |
| Letters only | ^[A-Za-z]+$ |
Smith |
Smith2, Smith Jones |
| Numeric only | ^[0-9]+$ |
123456 |
123-456, 12.5 |
Trimming leading and trailing spaces
To accept a value only when it has no leading or trailing spaces, include the space rules explicitly. A common way is to require the value to start and end with a non-space character:
| Purpose | Regex | Matches | Does not match |
|---|---|---|---|
| Text with no leading/trailing spaces, letters only | ^[A-Za-z]+( [A-Za-z]+)*$ |
John Smith |
John Smith, John Smith, John Smith |
| Any text, no leading/trailing spaces (at least one non-space character) | ^\S+( \S+)*$ |
John Smith, PF-123 |
John, John, |
| Alphanumeric with internal spaces allowed, trimmed ends | ^[A-Za-z0-9]+( [A-Za-z0-9]+)*$ |
Suite 12 |
Suite 12, Suite 12 |
What a space-only value does
A value made up only of spaces (for example " ") is not evaluated by a Regex validation — Regex only runs when the value is not empty. If the field must contain at least one non-space character, combine a Required validation with a Regex validation (for example ^\S+( \S+)*$). Note that a Required validation alone does not reject a space-only value.
What the end user experiences
- With Show Error Message, the value the user entered is kept and the error message is displayed next to the field as soon as the value does not match the pattern.
- With Trim characters based on Regex, leading/trailing spaces are removed and disallowed characters are cleaned out as the user types — the field simply never ends up in an invalid state.
- The form cannot be submitted until all Regex validations pass. When a field's error message is set, the field is highlighted and the message is shown.
Things to know
- Regex validation is available on fields displayed as String (see String data type) and works with the built-in formats (Email, URL, Phone) which are pre-defined patterns on the same field.
- Patterns are evaluated against the value only when it is not empty — an empty field does not fail a Regex validation (pair it with Required when the field must be filled in).
- If the field is configured with Bypass Validation, the user may continue to the next page with an invalid value, but will still be blocked from submitting the form.