Evaluate text fields with Regular Expression in Business Central

Hello reader, in this article I want to talk to you about a method for validating fields and text variables for your applications. You’ve probably heard of ‘Regular Expressions’, if you’ve never heard of them, I’ll leave you the link to the Wikipedia page where it explains the genesis and the rudiments of how it works.

I don’t want to bore you with how to construct a regular expression to evaluate a text-type input value, but I do want to draw your attention to the fact that in Business Central we have a codeunit that is right for us and that we can use whenever we are dealing with evaluating the meaning of a text field.

I’m referring to the 3960 Regex codeunit which is part of the System.Utilities namespace. In particular, this codeunit has a function called IsMatch which, given the input value and the pattern of the regular expression, returns true or false depending on whether the text value is compatible with the pattern or not.

Let’s see with a simple example, what field of application we can use it. Suppose we build a page with an input text field and an enum that is identical to the system on what scope we want to evaluate the input text. This enum has the following values:

The card page will have, therefore, the two text fields and the enum exposed and an action that allows you to call the EvaluateInput function of the codeunit that I created specifically for this example which is called Validate Input DZ (for convenience, in the print screen below I show only the action and the call to the function)

Now, let’s see what the EvaluateInput function looks like, it’s made up of a case statement that evaluates each element of the enum.

Leave out the regular expression pattern, that’s not the purpose of this post, it’s just to make you understand that we can evaluate a text value using the standard Regex codeunit. Anyway, if you want to know how I got the pattern back, I asked Copilot, as shown in the image below 😉

If you don’t have Copilot, I recommend that you visit this portal where you will be able to test your Regex101 patterns as well as consult the official Microsoft documentation for the Regex codeunit.

The result we will achieve will be this

This methodology, you can, for example, use it to evaluate input from external systems (external logistics files, banks, etc.), it is clear that for simple operations (understanding if a text value is a number for example) the use of the good old EVALUATE is always valid.

I hope this post has clarified for you, if you ever had any doubts, about using Regular Expressions in Business Central.

Leave a Reply