For  Email validation

Chapter - 08

Visit to buy the book on Citizen Development https://go.amitpuri.com/buynow

Code Snippet #4

For Email validation

This code snippet is designed to dynamically set the display mode of a control (such as a button) based on the validity of an email address entered in a text input control (txtFriendEmail). It comes in two versions: the first one with a specific validation criteria, and the second one with a more general approach.

    If(
        IsMatch(
            txtFriendEmail.Value,
            "^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$"
        ) && Last(
            ForAll(
                Split(
                    txtFriendEmail.Value,
                    "@"
                ),
                {Result: ThisRecord.Result}
            )
        ).Result in [
            "gmail.com",
            "outlook.com"
        ],
        DisplayMode.Edit,
        DisplayMode.Disabled
    )

    // or simply use, if you do not want to limit gmail.com or outlook.com
    // If(IsMatch(txtFriendEmail.Text, Match.Email), DisplayMode.Edit,DisplayMode.Disabled)

Explanation

Version 1: Specific Email Validation

  • Email Format Validation:- IsMatch(txtFriendEmail.Value, "^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$"): This function checks if the email entered in txtFriendEmail matches a regular expression pattern. The pattern validates the email format, ensuring it consists of:
    • User name part with alphanumeric characters, dots, underscores, or hyphens.
    • An @ symbol.
    • Domain name with alphanumeric characters or hyphens, followed by a dot.
    • A domain extension of 2 to 4 characters.
  • Domain Specific Validation:- The Last(ForAll(Split(txtFriendEmail.Value, "@"), {Result: ThisRecord.Result})).Result in ["gmail.com", "outlook.com"] part further checks if the domain of the email address (the part after @) is either gmail.com or outlook.com.
    • This is achieved by splitting the email address at @, iterating through the resulting parts with ForAll, and then checking if the last part (the domain) is one of the specified domains.
  • Setting Display Mode:- If(..., DisplayMode.Edit, DisplayMode.Disabled): This sets the display mode of the control. If both conditions are true (the email is in the correct format and the domain is either gmail.com or outlook.com), the mode is set to DisplayMode.Edit, making the control editable or interactive. If not, the mode is DisplayMode.Disabled, making it non-interactive.

Version 2: General Email Validation

  • If(IsMatch(txtFriendEmail.Text, Match.Email), DisplayMode.Edit, DisplayMode.Disabled): This is a simplified version using Power Fx’s built-in Match.Email pattern to validate the email format. If the email format is valid, it sets the control to DisplayMode.Edit; otherwise, it sets it to DisplayMode.Disabled.

Summary

The first version is useful when you want to restrict valid email addresses to specific domains (gmail.com and outlook.com in this case) and ensure they adhere to a specific format. The second version is a more general approach, suitable when any valid email format is acceptable, regardless of the domain. Both methods are used to control the interactivity of an element in the app based on the validity of the email address input.

Happy #low-code learning

Visit: www.amitpuri.com

Id: Chapter-08-CS00004

Category: Chapter 08

Amit Puri, Advisor and Consultant, Strengthening Digital Experiences, Modernize Cloud Journey with AI-Driven Transformation!

Code Snippet # 3
Chapter-08-CS00003 - Code Snippet # 3
Code Snippet # 1
Chapter-08-CS00001 - Code Snippet # 1
Code Snippet # 9
Chapter-06-CS00009 - Code Snippet # 9