Table of Contents

Regex formulas: check that an answer follows a pattern

Desirée M Updated by Desirée M

Regex formulas check that a visitor's answer follows a pattern, such as five digits for a postal code, hours and minutes for a time, or a company email address. The Formulas block has one regex formula, RegexTest, and it answers True or False, so the agent can accept the answer or ask for it again.

To add the block and write your first formula, see How to use the Formulas block: a beginner's guide. Prefer watching? The Formulas video tutorial in the Landbot Academy builds formulas step by step.

How does RegexTest work?

RegexTest(/pattern/, text) gives True when the text matches the pattern, and False when it doesn't. Write the pattern first, between two slashes, and the text second, usually a Field such as @cap. With the two swapped, RegexTest answers False.

A pattern describes what the text looks like, character by character:

● ^ and $: the start and the end of the text.

● \d: one digit, and {5} right after it means exactly five of them.

● [ ]: one character from the list, such as [67] for a 6 or a 7, or [0-5] for a digit from 0 to 5.

● ?: the part before it is optional, and | means "or".

● \/: a slash inside the pattern, such as in a web address or a date. Write each / this way.

Start a pattern with ^ and end it with $. Without them, an answer only needs to contain the pattern somewhere: 119:30:05x passes a time check that has no ^ and $.
RegexTest is case sensitive, and the builder ignores an i after the closing slash. To accept any capitals, wrap the Field in Lower() and write the pattern in lowercase.

How to use RegexTest in a Formulas block

RegexTest answers True or False, so pick TRUE / FALSE under Block outputs and save the result in a TEXT Field. Connect True to the next step. Connect False to a message that shows the format you expect, and then back to the question, so the visitor can try again.

The example below comes from one pizzeria agent, Forno Rosa, which checks the delivery details before taking an order.

Check a postal code, a mobile number and a delivery time

Forno Rosa asks three questions with Ask a question blocks, and a Formulas block after each one checks the answer with TRUE / FALSE. The first checks the postal code with RegexTest(/^\d{5}$/, @cap):

● True, such as 20121: the agent asks "What mobile number can the rider call when they arrive?"

● False, such as 201: "Postal codes in Italy have 5 digits, like 20121. Could you check it?" and the postal code question comes back.

Formulas block checking that the postal code has five digits, with False leading to a message and back to the question

The other two checks work the same way:

● Mobile number: RegexTest(/^(\+39|0039)? ?3\d{2} ?\d{6,7}$/, @phone) accepts 347 1234567 or +39 347 1234567. For 06 1234567, the agent says "That doesn't look like an Italian mobile number. It starts with 3, like 347 1234567."

● Delivery time: RegexTest(/^([01]\d|2[0-3]):[0-5]\d$/, @delivery_time) accepts 19:30. For 7pm, the agent says "Please write the time with hours and minutes, like 19:30."

When all three answers pass, the agent confirms: "Perfect! Your pizza goes to 20121 at 19:30. 🛵"

Web chat asking for the postal code again after a three-digit answer, then moving on after a five-digit one

Which patterns can you copy?

Each pattern below was tested with one answer that should pass and one that shouldn't. Replace @answer with your own Field.

Numbers, names, amounts and yes

● Numbers only: RegexTest(/^\d+$/, @answer). 12345 passes, 12a45 doesn't.

● An exact number of digits: RegexTest(/^\d{5}$/, @answer). Change 5 to the length you need.

● Letters, accents and spaces, for a name: RegexTest(/^[a-zA-ZÀ-ÿñÑ ]+$/, @answer). José Núñez passes, Jose3 doesn't.

● An amount with up to 2 decimals, with a comma or a dot: RegexTest(/^\d+([.,]\d{1,2})?$/, @answer). 12,50 passes, 12.555 doesn't.

● Yes, in different words and any capitals: RegexTest(/^(yes|yeah|yep|y|sí|si)$/, Lower(@answer)). Yeah passes, maybe doesn't.

Times and dates

● Time, HH:mm: RegexTest(/^([01]\d|2[0-3]):[0-5]\d$/, @answer). 19:30 passes, 25:00 doesn't.

● Time with seconds, HH:mm:ss: RegexTest(/^([01]\d|2[0-3]):[0-5]\d:[0-5]\d$/, @answer). 19:30:05 passes, 19:30 doesn't.

● Date, YYYY-MM-DD: RegexTest(/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/, @answer). 2026-11-25 passes, 2026-13-01 doesn't.

● Date, DD/MM/YYYY or DD-MM-YYYY: RegexTest(/^(0[1-9]|[12]\d|3[01])[-\/](0[1-9]|1[0-2])[-\/]\d{4}$/, @answer). 25/11/2026 passes, 11/25/2026 doesn't.

● Date, MM/DD/YYYY or MM-DD-YYYY: RegexTest(/^(0[1-9]|1[0-2])[-\/](0[1-9]|[12]\d|3[01])[-\/]\d{4}$/, @answer). 11/25/2026 passes, 25/11/2026 doesn't.

Postal codes

● Italy: RegexTest(/^\d{5}$/, @answer). 20121 passes, 2012 doesn't.

● Spain: RegexTest(/^(0[1-9]|[1-4]\d|5[0-2])\d{3}$/, @answer). 28013 passes, 53000 doesn't.

● France: RegexTest(/^(([0-8]\d)|(9[0-5]))\d{3}$/, @answer). 75008 passes, 96000 doesn't.

● United Kingdom: RegexTest(/^[A-Za-z]{1,2}\d[A-Za-z\d]? ?\d[A-Za-z]{2}$/, @answer). SW1A 1AA passes, SW1A 1A doesn't.

● United States, ZIP or ZIP+4: RegexTest(/^\d{5}(-\d{4})?$/, @answer). 90210-1234 passes, the Canadian K1A 0B1 doesn't.

● Canada: RegexTest(/^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/, @answer). K1A 0B1 passes, 90210 doesn't.

● Brazil: RegexTest(/^\d{5}-?\d{3}$/, @answer). 01310-100 passes, 0131-100 doesn't.

Phone numbers

● Italian mobile, with or without +39 and spaces: RegexTest(/^(\+39|0039)? ?3\d{2} ?\d{6,7}$/, @answer). +39 347 1234567 passes, 06 1234567 doesn't.

● Spanish mobile, with or without +34 and spaces: RegexTest(/^(\+34|0034)? ?[67]( ?\d){8}$/, @answer). +34 612 345 678 passes, 912 345 678 doesn't.

Emails, websites, references and IDs

● Company email only, for a B2B lead form: RegexTest(/^[\w.+-]+@(?!(gmail|hotmail|yahoo|outlook)\.)[\w-]+(\.[\w-]+)+$/, Lower(@email)). sofia@fornorosa.it passes, Sofia@Gmail.com doesn't. Add more free email providers inside the brackets, separated by |.

● Website: RegexTest(/^https?:\/\/[\w-]+(\.[\w-]+)+(\/\S*)?$/, @answer). https://fornorosa.it/menu passes, fornorosa.it doesn't.

● Order or booking reference, two capital letters, a dash and 4 to 6 digits: RegexTest(/^[A-Z]{2}-\d{4,6}$/, @answer). FR-1234 passes, FR1234 doesn't.

● Spanish DNI or NIE, format only: RegexTest(/^(\d{8}|[XYZ]\d{7})[A-Z]$/, @answer). X1234567L passes, 1234567L doesn't. The pattern doesn't check the control letter.

Can you use these patterns in an Ask a question block?

Yes. The Ask a question block has a Regex pattern setting that refuses an answer that doesn't match and shows the Validation error message, with no Formulas block. Paste only the part of the pattern between the two slashes: ^\d{5}$ for a postal code, not RegexTest(/^\d{5}$/, @answer).

Don't include the two slashes in the Regex pattern setting. With /^\d{5}$/, no answer matches and visitors can't go past the question.

The setting works a little differently from RegexTest:

● Capitals: the setting is case sensitive and has no Lower(), and (?i) refuses every answer. So the yes pattern refuses YES, and the company email pattern lets Sofia@Gmail.com through. Use RegexTest with Lower() for those two.

● Start and end: keep ^ and $ in the pattern. Without them, an answer with extra text at the end gets through, such as 20121x for \d{5}.

Use RegexTest in a Formulas block when the agent needs a different path for a wrong answer, or has to ignore capitals.

Which formulas work well with regex formulas?

Regex formulas are often combined with two other types:

● String formulas prepare the answer first, such as Lower for capitals.

● Logical formulas combine several checks into one, such as Or to accept a postal code from two countries.

Need help writing a formula?

The Formula Helper below is a Landbot agent that corrects formulas and explains how to write them.

Was this article helpful?

How to use the Formulas block: a beginner's guide

Date formulas: get today's date, add time and turn text into dates