Validate field pattern with Regular Expression in Dynamics 365 Business Central

When accepting user input in a field, it may be necessary to validate that the value matches a particular pattern. In early versions of Microsoft Dynamics NAV, you could use the RegEx dotNet libraries to validate input against a regular expression.

The RegEx dotNet libraries are not directly available in Dynamics 365 Business Central; however, there is a RegEx Codeunit that provides functionality to use regular expressions to match text patterns. The RegEx Codeunit implements an IsMatch procedure, which you can use to validate a value against a Regular Expression.
For example, to match a pattern of three alphabetic characters followed by a hyphen and then any three alphanumeric characters (Good: ABC-D1Q, Bad: 123-ADS), the following RegEx example implementation demonstrates the use of the IsMatch procedure:

 Adjust the pattern and code to satisfy your requirements. Text version of code listed below.

field(50101; "DVLPR RegEx Code"; Code[20])
            Caption = 'RegEx Code';
            DataClassification = CustomerContent;

            trigger OnValidate()
                Matches: Record Matches;
                Regex: Codeunit Regex;
                Value : Text;
                Pattern := '[A-Z]{3}\-[0-9,A-Z]{3}

                if Regex.IsMatch("DVLPR RegEx Code", Pattern, 0) then
                    Error('No Match');

Note: The code listed in this article is for demonstration purposes only 

Permanent link to this article:


    • Matt on November 1, 2022 at 4:42 pm
    • Reply

    the matches table and the regex codeunit don’t seem to be available in version 21

    1. Hi Matt – I have used both Codeunit 3960 – “RegEx” and Table 3965 – “Matches” in Build 21.0.46256. I see them both in the System Application.

Leave a Reply

Your email address will not be published.