Table of Contents

Formulas - Regex

Dilyara Updated by Dilyara

If is your first time with Formulas, please check our Getting Started guide here and our crash course here
Stuck? Try the formula helper below

Formulas available

  • RegexText

RegexTest

Check if the user input contains only numbers and no other characters

In a case where you want to validate that the input from a user contains numbers only, what we need to do is store in Landbot variable (in this example we call it @user_number), then use the Formula block:

RegexTest(/^\d+$/,@user_number)

Check for numbers of 11 digits

In a case where you want to validate that the input from a user has 11 digits, what we need to do is store in Landbot variable (in this example we call it @code_number), then use the Formula block:

RegexTest(/^\d{11}$/,@code_number)

Note: We want to validate if is TRUE or FALSE, that the number matches our pattern, so we will set up the Block Outputs as TRUE/FALSE

You'll be able to set in the builder bot:

  • The True output (green arrow) - to confirm the user that the number is in the correct format and has the correct number of digits.
  • The False output (red arrow) - to send a message explaining that it's incorrect and connects this message with the text block to start the flow again (asking for the number). 
Check if the input is a valid UK postal code
RegexTest(/([A-PR-UWYZ][A-HK-Y0-9](?:[A-HJKS-UW0-9][ABEHMNPRV-Y0-9]?)?\s*[0-9][ABD-HJLNP-UW-Z]{2}|GIR\s*0AA)/,@text)

Check if the input is a valid US zip code

RegexTest(/^(^[A-Za-z]\d[A-Za-z][ ]?\d[A-Za-z]\d$)|(^\d{5}$)/, @text)

Check if the input is a valid Canadian postal code
RegexTest(/^([A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d)/,@text)

Check if the input is a valid Brazilian postal code

In this case, it accepts both XXXXX-XXX and XXXXXXXX formats:

RegexTest(/^\d{5}-?\d{3}$/,@text)

Check if the input is a valid French postal code
RegexTest(/^(([0-8][0-9])|(9[0-5]))[0-9]{3}$/,@text)
Check if the input contains a word that is "stop" or something that starts with "stop"

RegexTest(/^stop.*/,@text)

Check if the input contains only letters

In this case, spaces are allowed:

RegexTest(/^[a-zA-Z\s]*$/, @text)
Check if the input contains only letters, including special characters and accent

RegexTest(/^[a-zA-ZÀ-ÿ\u00f1\u00d1]+(\s*[a-zA-ZÀ-ÿ\u00f1\u00d1]*)*[a-zA-ZÀ-ÿ\u00f1\u00d1]*$/,@text)
Check if the input time input is in format HH:mm:ss

In this example, in the variable @text we are storing the time by the user

RegexTest(/(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)/,@text)

Check if the time input is in format HH:mm
RegexTest(/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/, @text)
Check if the date input is in format yyyy-mm-dd

In this example, in the variable @date we are storing the date by the user

RegexTest(/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/,@date)

Check if the date input is in format mm-dd-yyyy
RegexTest(/^(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])-\d{4}$/,@text)

Check if the date is in format dd-mm-yyyy
RegexTest(/^(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[0-2])-\d{4}$/,@text)
Check if the input is in format of South African ID number
RegexTest(/(((\d{2}((0[13578]|1[02])(0[1-9]|[12]\d|3[01])|(0[13456789]|1[012])(0[1-9]|[12]\d|30)|02(0[1-9]|1\d|2[0-8])))|([02468][048]|[13579][26])0229))(( |-)(\d{4})( |-)(\d{3})|(\d{7}))/,@text)

Check if the input is a valid French VAT number
RegexTest(/^[A-Za-z]{2,4}(?=.{2,12}$)[-_\s0-9]*(?:[a-zA-Z][-_\s0-9]*){0,2}$/, @text)
Check if uploaded file contains image
RegexTest(/(.*?)\.(jpg|JPG|png|PNG|jpeg|JPEG)$/, @file)

Check if the input contains Italian country code

In this case, the correct format will start with "+39".

RegexTest(/^(([+])39)/,@text)

Check if the input is a valid Spanish phone number

In this case, the country code is optional:

RegexTest(/^(\+34|0034|34)?[ -]*(6|7)[ -]*([0-9][ -]*){8}/,@phone)

Check if the input is a valid Italian phone number
RegexTest(/^(([+]|00)39)?((3[1-6][0-9]))(\d{7})$/,@phone)

Formula Helper

Was this article helpful?

Formulas Blocks Dashboard

Formulas - Date

Contact