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})+$")
: - thisIsMatch
function checks whether the text intxtFriendEmail
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"]
: - thisSplit(txtFriendEmail.Text, "@")
function splits the email address at the@
symbol. TheLast(...)
function gets the last part of the split result, which is the domain of the email address. This part checks if the domain is eithergmail.com
oroutlook.com
. If(..., DisplayMode.Edit, DisplayMode.Disabled)
: - This is anIf
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 eithergmail.com
oroutlook.com
), the display mode is set toDisplayMode.Edit
. If either of the conditions is false, the display mode is set toDisplayMode.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
Dr. Amit Puri, Advisor and Consultant, Strengthening Digital Experiences, Modernize Cloud Journey with AI-Driven Transformation!