When building apps, you want to make sure your data is stored correctly into your Data Source. Especially when you’re storing specific data types (such as email address, telephone number, etc.)

In this blogpost, I will explain how you validate your data types.

When validating data, the IsMatch function should be your best friend. Using this allows you to check data input to comply to a specific format.

The function is being called as follows:

IsMatch(
	<CONTROL>,
	<PATTERN>
)
  • <CONTROL> is the output value of the control you want to validate (e.g. TextInput1.Text)
  • <PATTERN> is the validation pattern to be used. The IsMatch function allows you to use predefined patterns, provided to you by Microsoft. It also allows you to use custom regular expressions (Regex) if the predefined patterns don’t exactly match your needs. More on these patterns below.

The IsMatch function returns a boolean value (true/false) on which you can validate your data.

Predefined patterns

Predefined patterns are basically regular expressions, but in a more ‘readable’ form. These patterns can be called by using the Match.<PATTERN> method.

A predefined pattern that I’m using very often is the Match.Email pattern that checks if the input data corresponds to an email format:

IsMatch(
	txtEmailAdress.Text,
	Match.Email
)

There are a few more predefined patterns that you can use. For more info on these patterns, check this article.

Regular expressions (Regex)

If you’re looking for a validation that’s not provided by a predefined pattern, you can also use custom regular expressions for this. There are a lot of examples available on the web if you Google Bing search for regular expressions. Another good source is the Regular Expression Library. If you have found/configured a regular expression that fits your need, you can test this expression on regex101.

The syntax is just the same as the predefined pattern for IsMatch, but instead of using Match.<PATTERN> for your pattern, you define the regular expression directly as a string:

IsMatch(
	txtPhoneNumber.Text,
	"^(?([+]31|0031|0)-?6(s?|-)([0-9]s{0,3}){8}$"
)

This will validate the input of the txtPhoneNumber control to a Dutch mobile phone number (starting with +31, 0031 or 0, followed by 6, a space or dash (optional) and finally 8 number characters (0-9))

In the examples below, I configured a simple Power App with 2 text input controls which will ask for email address and telephone number.

Match.<PATTERN>

The Email address control will validate through the Match.Email predefined pattern as stated above. This pattern is configured on the Visible attribute of the validation label (which contains the red text) as follows:

If(
    IsMatch(
        txtEmailAddress.Text,
        Match.Email
    ),
    false,
    true
)

If the input of the txtEmailAddress control matches the Match.Email pattern, the label will not be shown. Otherwise, it will.

Regular expression

The Phone number control will validate through the regular expression as stated above on the Visible attribute of the validation label as follows:

If(
    IsMatch(
        txtPhoneNumber.Text,
        "^(?([+]31|0031|0)-?6(s?|-)([0-9]s{0,3}){8}$"
    ),
    false,
    true
)

Now that you know how to validate your data, you can use this to prevent users from submitting incorrect data types.

Button validation

When using a button to submit your data, you can configure this button to only be clickable when all input is in the correct format. You can do this by configuring the DisplayMode attribute of the button to check whether the validation labels are visible or not:

If(
    Or(
        lblEmailAdressValidation.Visible,
        lblPhoneNumberValidation.Visible
    ),
    DisplayMode.Disabled,
    DisplayMode.Edit
)

If one or more validation labels are visible, the button will be disabled:

Power Apps forms validation

When using a Power Apps form, you cannot always use a button to submit your data (e.g. when using a SharePoint integrated form, because the Save button is outside of the Power App context). In these cases, you need to figure out another way of validating your data.

I’ve configured a simple SharePoint list with a Email address and Phone number field, including the same validation labels as configured above and configured it to have a Power App as an integrated form:

OnSave

The easiest way to do this (when using a SharePoint integrated form) is to use the OnSave attribute of the SharePointIntegration element. The default value of this attribute is just a simple SubmitForm action:

SubmitForm(SharePointForm1)

You can alter this to only use this formula when the validation labels are not visible:

If(
    And(
        Not(lblEmailAdressValidation.Visible),
        Not(lblPhoneNumberValidation.Visible)
    ),
    SubmitForm(SharePointForm1)
)

Update

Another method is to configure the Update attribute of each single DataCard to check whether the validation label inside the DataCard is visible or not. If the label isn’t visible, it means that the data type is correct and we can just update the value of the corresponding control to the Data Source. If it isn’t correct, we need to pass a blank value to the Data Source which will result in a form validation error.

If(
    lblPhoneNumberValidation.Visible,
    Blank(),
    DataCardValue2.Text
)

Please note that this solution only works if your DataCard is set to required!
Otherwise, the blank value will just be submitted to your Data Source.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.