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 intxtFriendEmail
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 eithergmail.com
oroutlook.com
.- This is achieved by splitting the email address at
@
, iterating through the resulting parts withForAll
, and then checking if the last part (the domain) is one of the specified domains.
- This is achieved by splitting the email address at
- 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 eithergmail.com
oroutlook.com
), the mode is set toDisplayMode.Edit
, making the control editable or interactive. If not, the mode isDisplayMode.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-inMatch.Email
pattern to validate the email format. If the email format is valid, it sets the control toDisplayMode.Edit
; otherwise, it sets it toDisplayMode.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
Amit Puri, Advisor and Consultant, Strengthening Digital Experiences, Modernize Cloud Journey with AI-Driven Transformation!