Regex validation for form fields

What regex validation does

A regex validation rule checks a field's answer against a regular expression before the form is submitted. A match means the answer is valid. If the answer does not match, the form shows the rule's validation message and prevents completion until the user corrects it.

Regex validation runs in the form itself. It is useful for allowed-character rules and predictable formats, such as reference numbers, employee identifiers or names that may contain only an approved set of punctuation.

By default, 3B Forms runs field validation when the user completes the form. A form with a different validation mode already stored in its configuration may show the message earlier, such as after an answer changes.

Add a regex rule

  1. Open the form in the 3B Forms builder.
  2. Select the question you want to validate.
  3. In the question properties, find Validation rules and select Add new rule.
  4. Choose Regex as the rule type.
  5. Enter the pattern in Regular expression. Enter only the pattern, without surrounding / characters.
  6. Enter a clear Notification message for invalid input, for example Use letters, numbers, spaces, commas, slashes, apostrophes, colons, ampersands, parentheses or hyphens only.
  7. Use Case insensitive only when the pattern should treat uppercase and lowercase letters as equivalent. The example pattern below already includes both uppercase and lowercase letters, so it does not need this setting.
  8. Preview the form and test both accepted and rejected values before publishing the version.

If the field must contain an answer, also mark the question as required. An empty answer is not checked by the regex validator, so regex validation does not replace the required setting.

Allow alphanumeric characters and selected punctuation

Use this pattern to allow English letters, numbers, spaces, commas, forward slashes, straight apostrophes, colons, ampersands, parentheses and hyphens:

^[a-zA-Z0-9 ,/':&()-]*$

This is equivalent to the more heavily escaped form below:

^[a-zA-Z0-9 ,\/':\&()\-]*$

The first version is easier to read when entered directly in the builder. A forward slash does not need to be escaped because the builder expects the pattern without /.../ delimiters. The ampersand also has no special meaning in a regular expression, and the hyphen is treated literally because it is the last character in the character set.

How the pattern works

Part Meaning
^ Start of the answer
[a-zA-Z0-9 ,/':&()-] One character from the allowed set
* Zero or more allowed characters
$ End of the answer

The ^ and $ anchors are important. Together they require the whole answer to use allowed characters. Without them, an answer can pass when only one part of it matches.

The * allows any number of the approved characters. It also allows an empty string, but 3B Forms skips regex evaluation for an empty answer in any case. Use the question's required setting when an answer must be provided.

Examples

With the pattern above, these answers are valid:

Answer Why it is valid
John Smith Letters and a space
Ward 2, North Letters, a number, a comma and spaces
O'Brien Letters and a straight apostrophe
R&D Letters and an ampersand
Agency/Bank Letters and a forward slash
Unit (West) Letters, spaces and parentheses
Temp-Staff Letters and a hyphen

These answers are invalid unless you add the relevant characters to the pattern:

Answer Rejected character
Smith.Jones Full stop (.)
Room_4 Underscore (_)
user@example.com At sign (@) and full stop (.)
José Accented letter (é)
John “Jack” Smith Curly quotation marks

The pattern allows only the English letters a to z and A to Z. It does not allow accented letters or characters from other alphabets. It also accepts spaces anywhere in the answer, including at the beginning or end.

Adapt the allowed characters

Add a character inside the square brackets when it should be accepted. For example:

Requirement Pattern
Also allow full stops ^[a-zA-Z0-9 ,./':&()-]*$
Also allow underscores ^[a-zA-Z0-9 _,/':&()-]*$
Allow letters and numbers only ^[a-zA-Z0-9]*$
Allow digits only ^[0-9]*$

Be careful when adding characters that have a special regex meaning. In particular, keep a literal hyphen at the end of the square brackets or escape it as \-. Test every amended pattern in Preview with characters immediately on both sides of the allowed and rejected boundary.

Write a useful validation message

The validation message should tell the user how to fix the answer. Avoid messages such as Invalid value or exposing the regular expression itself.

For the example rule, use a message such as:

Use letters, numbers, spaces, commas, slashes, apostrophes, colons, ampersands, parentheses or hyphens only.

If the audience may enter accented names or typographic punctuation, expand the pattern rather than asking users to alter their names. Keep the message aligned with the characters the pattern actually accepts.

Test the rule

In Preview, check at least the following:

Regex validation and Salesforce validation rules serve different purposes. The regex rule checks answers entered through this form. If the same Salesforce field can be populated through imports, integrations, flows or other interfaces, enforce any organisation-wide data requirement in Salesforce as well.

Troubleshooting

Problem What to check
A blank answer is accepted Mark the question as required. Regex rules do not validate empty answers.
An invalid answer is accepted Confirm the pattern starts with ^ and ends with $, then test the published form version.
A valid answer is rejected Look for a character that is not in the square brackets, especially accented letters, curly punctuation, tabs or line breaks.
A slash causes confusion Enter the pattern without surrounding / delimiters. A slash inside the allowed-character set can be entered as /.
Users cannot tell how to fix the answer Replace the notification with a specific list or example of the accepted characters.
Preview and the public form behave differently Confirm that the version tested in Preview is the version used by the published form link.

For the wider submission sequence and Salesforce-side errors, see Validation, submission and error handling.