Dynamic Regular Expression (RegEx) pattern match using IsMatch(), Match() in PowerApps

What is Issue?

PowerApps has few functions like IsMatch(), Match() functions to validate the fields using RegEx. I have to validate the phone numbers, postal codes on lots of screens using RegEx. There are lots of screen where I have to validate fields with similar Regular Expression (RegEx).

I wrote the IsMatch() function on each error message label text and visible event on respective text fields to show error message if input text is not as per RegEx. I published the app when ready.

Later, I got the updated ReqEx from business. Here, I realized the IsMatch() function is having constant RegEx and I have to update each function manually again.

I thought of declaring the variables for each RegEx OnStart of App or OnVisible of main screen. When I tried putting variable in place of RegEx string, formula started showing error and not allowing me to use the already declared RegEx variables.

The Reason-

IsMatch() function do not support dynamic RegEx directly. Only constant or Enum allowed as RegEx pattern in IsMatch() , Match() functions.

Solution-

PowerApps has lots of capabilities for fast and quality application development. But there are few limitations on PowerFx/Functions available in PowerApps. While developing the app, one challenge every developer faces – Dynamic and optimized coding to make application much configurable with very little changes.

Suppose you have 10 screens in power app application and each/most of the screens are has same functionality or same validations. So you have to repeat the each steps manually on each screen or you can create reusable component for the same.

Let’s see how we can make IsMatch(), Match() functions to support dynamic RegEx.

Here I am going to create simple custom component.

Let’s take Example –

I have to validate phone number in multiple screens with below pattern –

^(\+\d{1,2}\s?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$

It will accept only below phone number formats –

123-456-7890
(123) 456-7890
123 456 7890
123.456.7890
+91 (123) 456-7890

Follow the below steps –

  • Create Simple PowerApps custom component “RegExFormatter” with a text input
    • RegEx
  • IsMatch() function Supports only Text as RegEx pattern. Hence, create a function output with Text return type
    • GetFormattedRegEx
  • You can refer the blog for creation of custom component –MS Dynamics 365 : Component Library- Reusable Components for Power Apps
  • In output function GetFormattedRegEx() add the below code-
With( { expression : "\w*" }, Self.RegEx & expression )

“\w*” is to validate any word and appending it with input RegEx will help to convert any RegEx into Text format.

  • Publish the custom Component.
  • Import RegExFormatter component in App/Custom Page
  • Now Let’s start configuring variables in App/Custom Page.
  • OnVisible of Screen or OnStart of App/Custom Page declare the variables
    • Global Variable – var_PhoneRegEx : with pattern –
      • ^(+\d{1,2}\s?)?(?\d{3})?[\s.-]?\d{3}[\s.-]?\d{4}$
    • Context Variables – var_FieldChanged : to identify changed field
  • Configure RegExFormatter component in App/Custom Page-
  • Add input parameter
    • RegEx = var_PhoneRegEx
  • Update OnChange event of the phone fields on each form to update context variable –
    • var_FieldChanged = “Account_Phone” (you can assign any text value to identify the OnChange event for respective error message field.)
  • Update error label Text and Visible event by calling the component function output in IsMatch() function. Example-
!IsMatch(DataCardValue2.Value,RegExFormatter_1.GetFormattedRegEx(),MatchOption.Complete)
  • IsMatch() will treat returned value of function RegExFormatter_1.GetFormattedRegEx() as constant text.
  • Yay.. No error this time for IsMach() function.
  • Save and test the app.

In case there is change in RegEx for particular validation , we can just update RegEx variables in OnStart or OnVisible events only.

Now we have created truly dynamic RegEx matcher and resolved the constant string issue.

You can use the same for gallery control if you have the input boxes and validations using RegEx. You can handle error visibility in gallery by adding extra column (isValid) in gallery data source. You have to configure the variable properly.

Hope this will help…
Enjoy MS CRM!!!

Follow on Facebook- FB: MSCRM16Tech

Leave a comment