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()
var
Matches: Record Matches;
Regex: Codeunit Regex;
Pattern,
Value : Text;
begin
Pattern := '[A-Z]{3}\-[0-9,A-Z]{3}
if Regex.IsMatch("DVLPR RegEx Code", Pattern, 0) then
Message('Match')
else
Error('No Match');
end;
}
Note: The code listed in this article is for demonstration purposes only
2 comments
the matches table and the regex codeunit don’t seem to be available in version 21
Author
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.