Ensuring data quality in Microsoft Dynamics 365 (D365) is essential, and one of the best ways to enforce input validation is by using regular expressions (regex) in JavaScript. This article will walk you through how to add regex validation to a form field in D365, preventing invalid user input.
Regular expressions allow us to define flexible validation rules for form fields. For example, you may want to restrict a field to only alphanumeric characters, enforce a minimum length, or ensure a phone number follows a specific pattern.
Implementing Regex Validation in D365
To apply regex validation to a field in a D365 form, follow these steps:
In my case, I applied this to the out-of-the-box (OOTB) Business Phone form field to ensure proper phone number formatting.
Add the JavaScript Code
Below is an example function that validates a field’s value using regex:
- Open the form’s properties.
- Choose the field where validation is needed.
- Under Events, select Event Handler
- Select Add library and add the JavaScript file.
- Pass the function pbs_regexCheckValue and the field’s logical name as a parameter.
Let's check:
I hope these instructions have helped some of you.