How to validate lead's input using regular expressions
Sometimes you want the user's input to follow a strict format, but it won't be logical to enter all the possible combinations for the user's input to check against them. That's where regular expressions come in handy. It allows you to validate the user's input against certain patterns. Also, using regular expressions with conditional logic allows you to change the conversation's path depending on the user's input.
Find out more about Conditional Logic here.
How to use Regular Expressions with Conditional Logic
The first step is to add the question you want to validate its answer, make sure to save the answer to a custom field.
After the question step add a Conditional Logic object to the bot and connect it. Pick the field you saved the answer to and set the condition to Regular Expression.
Add the required regex rule and save. Now the conditional logic will give you two path : True and False, the conversation will follow the True path when the value of that field matches the regex, if not it will follow the False path.
Let's take a practical example to explain this to you. Let's say we want the lead to enter their phone number in the US patten eg. (555) 555-1234, or similar patterns that looks the same but have different small changes, like the position of the space or the hyphen or the brackets.
Add the phone question and save its value to a custom field, we will call us usphone. Add a conditional logic to it and set the regular expression. The regex rule for the US pattern is :
\(?\d{3}\)?-? *\d{3}-? *-?\d{4}<br>
Examples of Regular Expressions rules
There are numerous regular expressions that you can use to validate the answers, you can even create your own rules. RegEx is a flexible language that you can adapt to your needs, you can use google to learn more about it and find more examples that you can use.
Here is a list of useful web pages that list useful regex rules: