Header Ads Widget

Top Picks


Regular Expression in Business Central - Validate fields using business central

Introduction to Regular Expression

Information can be easily extracted from any text using regular expressions (regex or regexp) by searching for one or more matches of a given search pattern (that is, a particular set of ASCII or Unicode letters).

Validation, parsing/replacing text, translating data to different formats, and web scraping are just a few of the many possible applications.

Once you're familiar with the syntax, this tool may be used in virtually any programming language (JavaScript, Java, Visual Basic, C#, C/C++, Python, Perl, Ruby, Delphi, R, Tcl, and many more), with only minor differences across languages and engine versions for the most sophisticated functionality.

Let's get started with some illustrations and clarifications.

Basic Characters

Anchors - ^ and $

^The        matches any string that starts with The -> Try it!

end$ finds strings that end with that character.

Exact string matching (begins and ends with The end) with the value "^The end$"

roar finds any string that contains the word "roar"

Using Business Central's Regular Expression to verify the field

Before accepting a value supplied by a user, it can be necessary to verify that it follows a specific pattern. The RegEx dotNet libraries were used in earlier versions of Microsoft Dynamics NAV to facilitate regular expression validation.

In Business Central we have RegEx codeunits as a base application.

Using Business Central's Regular Expression to verify the field

While Dynamics 365 Business Central does not include the RegEx dotNet libraries out of the box, a RegEx Codeunit is available for developers that need to use regular expressions to match text patterns. You can use the IsMatch technique built into the RegEx Codeunit to check if a given value satisfies a given Regular Expression.

Pattern := '[A-Z]{3}\-[0-9,A-Z]{3}

The above RegEx implementation example demonstrates the use of the IsMatch function to match a pattern consisting of three alphabetic characters followed by a hyphen and then any three alphanumeric characters (Good: GOM-123, Bad: 123-GOM):

We have to initialize the RegEx code unit in a variable as below

regex: Codeunit Regex;

The following code is used to validate the employee ID

Code snippet

field(EmpID; EmpID)


                    ApplicationArea = All;

                     trigger OnValidate()


                        Pattern := '[A-Z]{3}\-[0-9,A-Z]{3}';

                        if regex.IsMatch(EmpID, Pattern, 0) then



                            Error('Please enter the valid input');



The following code is used to validate the email id

field(email; email)


                    ApplicationArea = All;

                    trigger OnValidate()


                        Pattern := '^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$';

                        if regex.IsMatch(email, Pattern, 0)




                            Error('INvalid input');



Please check the complete code here 

Check the complete video here to know how to validate the field and implement the above code.

Leave your queries in the comment. 

Will meet you in another blog. 

Stay safe


Post a Comment


Youtube Channel Image
goms tech talks Subscribe To watch more Tech Tutorials