For  validating and enabling required control on the screen

Chapter - 06

Visit to buy the book on Citizen Development https://go.citizendeveloper.codes/buynow

Code Snippet #2

For validating and enabling required control on the screen

This Power Fx code is used within a Power Apps canvas app to control the display mode of a button based on the validation of an email address entered into a text input control (named txtFriendEmail).

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

Explanation

  • IsMatch(txtFriendEmail.Text, "^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$"): - this IsMatch function checks whether the text in txtFriendEmail matches the specified regular expression. The regular expression ^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$ is used to validate the format of an email address. It ensures the email:
    • Starts with alphanumeric characters, dots, underscores, or hyphens.
    • Contains an @ symbol.
    • Has a domain part with alphanumeric characters or hyphens followed by a period.
    • Ends with a top-level domain that is 2 to 4 characters long.
  • Last(Split(txtFriendEmail.Text, "@")).Result in ["gmail.com", "outlook.com"]: - this Split(txtFriendEmail.Text, "@") function splits the email address at the @ symbol. The Last(...) function gets the last part of the split result, which is the domain of the email address. This part checks if the domain is either gmail.com or outlook.com.

  • If(..., DisplayMode.Edit, DisplayMode.Disabled): - This is an If statement that sets the display mode based on the above conditions. If both conditions are true (the email is in the correct format and the domain is either gmail.com or outlook.com), the display mode is set to DisplayMode.Edit. If either of the conditions is false, the display mode is set to DisplayMode.Disabled, making the button disabled.

In summary, this code is used to enable or disable a button in a screen based on whether the email address entered in the txtFriendEmail text input is valid and belongs to either the gmail.com or outlook.com domains.

Happy #low-code learning

Visit: www.citizendeveloper.codes

Id: Chapter-06-CS00002

Category: Chapter 06

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

Code Snippet # 7
Chapter-08-CS00007 - Code Snippet # 7
Code Snippet # 1
Chapter-08-CS00001 - Code Snippet # 1
Code Snippet # 4
Chapter-06-CS00004 - Code Snippet # 4