Regular Expressions for Validation | Canvas Apps



Introduction

Power Apps Canvas apps allow developers to create custom applications with a rich user interface. One common requirement is to validate user input before processing it further. Regular expressions provide a flexible way to define complex patterns for validation. In this blog post, we’ll explore how to use regex expressions to apply validations and rules within a Canvas app.

Prerequisites

Before we begin, make sure you have a basic understanding of Power Apps and its components.

1. Email Validation

Let’s start with a practical example: validating email addresses. We want to ensure that users enter valid email addresses in a text input control. Here’s how you can achieve this using regex:

  1. Create a Text Input Control: Add a text input control to your Canvas app where users can enter an email address.

  2. Apply Regex Validation:

    • Use the IsMatch function to check if the entered email matches a valid email pattern.
    • The following regex pattern validates email addresses:
      ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
      
    • This pattern ensures that the email address contains an “@” symbol, a domain name, and a valid top-level domain (TLD).
  3. Implement Validation Logic:

    • In the OnSelect property of a button (e.g., a “Submit” button), write the following code:
      UpdateContext({
          varIsValidEmail: If(
              IsMatch(txtEmail.Text, "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"),
              true,
              false
          )
      })
      
  4. Display Validation Feedback:

    • Show an error message if varIsValidEmail is false.
    • Otherwise, proceed with further actions.

2. Custom Validations

Apart from email validation, you can create custom validations using regex for other scenarios:

  • Phone Numbers: Validate phone numbers based on country-specific formats.
  • ZIP Codes: Ensure ZIP codes match the expected format.
  • Credit Card Numbers: Validate credit card numbers using regex patterns.
  • Custom Patterns: Define your own regex patterns for specific requirements.

3. Advanced Techniques

  • Multiple Validations: Combine multiple regex patterns using logical operators (AND, OR) to perform complex validations.
  • Extracting Matched Text: Use the Matches function to extract parts of the input that match a pattern.
  • Dynamic Patterns: Allow users to define their own validation rules by storing regex patterns in a data source.
  • Reference Solution: You can import my Canvas App solution which demonstrates an advanced way of applying custom validations in Canvas Apps:

    https://github.com/shaheerahmadch/Advanced-Validations-UDF 


Conclusion

Regular expressions provide a robust way to validate user input in Power Apps Canvas apps. By leveraging regex, you can enforce data quality and improve the overall user experience. Remember to test thoroughly and provide clear feedback to users when validation fails.

Happy app-building! 🚀



References:

  1. Email Validation with RegEx in PowerApps Canvas app1
  2. Email validation using RegEx in PowerApps2
  3. Email regex validation3
  4. More Regular Expressions for Canvas apps4

Comments