There a PowerApps canvas app, where we have rich text field, where we can add the hyperlink in the text field.
I need a code where error should be thrown if there is any link present in the rich text field during the submission of the form.
I tried this code, it is not working,
IsMatch(RichTextEditor1.HtmlText,”https://”), Notify(
“Please dont give hyperlinks in the field”,
NotificationType.Error,
3600)
By default the IsMatch function will try to match the regular expression with the entire first argument. If you want to check whether it just contains value “https://” you can use the third argument (https://learn.microsoft.com/power-platform/power-fx/reference/function-ismatch#match-options) to tell it that:
If(
IsMatch(RichTextEditor1.HtmlText, "https://", MatchOptions.Contains),
Notify("Please dont give hyperlinks in the field", NotificationType.Error, 3600)
)
Notice that this regular expression may be catching more than just links: for example, if the text contains a link like in the image below, the logic will also flag it as if it had hyperlinks:
If this is good enough for you, then great.
Otherwise you can harden the regular expression to make it stricter (something like IsMatch(RichTextEditor1.HtmlText, "href=""https?://",MatchOptions.Contains)
, but even that can be “defeated” by a regular text. You can always tighten it up (for example, accounting for the opening element <a
, account for both http and https, possible extra attributes, but make sure to test well your regular expression so that it only does catch what you want and does not catch what you don’t.