cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

How to recognize specific characters in the text string?

Hello all, 

 

I would like to check whether user has entered any invalid characters and if they did show them a warning or block some buttons.

To do this, I am trying to write the code shown below, but realized that using "||" to recognize characters does not work. 

 

Coalesce(Parent.Error, If( "*" || """" || "?"
in txt_Field.Text,
  "Please do not include any of these characters: "& Char(34)& " * : < > ? / \ |"))

 

 

That formula only respond to symbol "?", but not "*", or """".  Could someone please advice how to fix it, so that several characters would be recognized?

mantastg08876_0-1618550587230.png

 

3 ACCEPTED SOLUTIONS

Accepted Solutions
WarrenBelz
Super User
Super User

Hi @Anonymous ,

Put this on the OnChange of the Text Box

With(
   {
      wSymbol: Table(
         {Value: "*"},
         {Value: ":"},
         {Value: "<"},
         {Value: ">"},
         {Value: "?"},
         {Value: "/"},
         {Value: "\"},
         {Value: "|"}
      )
   },
   ForAll(
      wSymbol,
      If(
         Value in Self.Text,
         Notify(
            "Please type text only here",
            Error
         );
         Reset(Self)
      )
   )
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

RusselThomas
Microsoft
Microsoft

Hi @Anonymous ,

The problem lies in the construct of your conditions - you can't string conditions by just stating the result of a condition.

For example;

If(firstName = "Bob" || "Sue" || "Frank")

won't work, but

If(firstname="Bob" || firstname="Sue" || firstname="Frank")

will work. 

Try this;

 

Coalesce(Parent.Error, If( "*"
in DataCardValue4.Text || "?" in DataCardValue4.Text || """" in DataCardValue4.Text, 
  "Please do not include any of these characters: "& Char(34)& " * : < > ? / \ |"))

 

 

 

If you're feeling adventurous, you can also use Match() or MatchAll() with a regex pattern to pick up special characters.  

 

Hope this helps,

 

RT

View solution in original post

Hi @Anonymous ,

Your post got me thinking there is a smarter way of doing this and it was something I might use myself, so I came up with this working model.

SpecialChar.gif

Firstly do a Collection of all special characters (you can do this at Screen OnVisible) by using their ASCII value ranges

ClearCollect(
   colChar,
   ForAll(
      Sequence(15),
      {FieldNo: Char(32 + Value)}
   ),
   ForAll(
      Sequence(7),
      {FieldNo: Char(57 + Value)}
   ),
   ForAll(
      Sequence(5),
      {FieldNo: Char(90 + Value)}
   ),
   ForAll(
      Sequence(4),
      {FieldNo: Char(122 + Value)}
   )
)

this puts 31 Special Characters into the collection under the field FieldNo.
Below is a gallery (with wrap at 4) showing the characters

WarrenBelz_1-1618640289481.png

Now put this on the OnChange of the Text Box

ForAll(
   colChar,
   If(
      FieldNo in Self.Text,
      Collect(
         colError,
         {CharError: true}
      );
      Reset(Self)
   )
)

and this on the OnSelect of the Text Box - you could also in addition put this at Screen OnVisible 

Clear(colError)

 Now put a Label (this is the warning message) on the screen with this as the Text

"Do not use characters " & Concat(
    colChar,
    FieldNo & ""
) & " here"

and this as the Visible

First(colError).CharError

NOTE - I had to use a Collection here as a Variable cannot be set inside a ForAll() statement.

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

4 REPLIES 4
WarrenBelz
Super User
Super User

Hi @Anonymous ,

Put this on the OnChange of the Text Box

With(
   {
      wSymbol: Table(
         {Value: "*"},
         {Value: ":"},
         {Value: "<"},
         {Value: ">"},
         {Value: "?"},
         {Value: "/"},
         {Value: "\"},
         {Value: "|"}
      )
   },
   ForAll(
      wSymbol,
      If(
         Value in Self.Text,
         Notify(
            "Please type text only here",
            Error
         );
         Reset(Self)
      )
   )
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

RusselThomas
Microsoft
Microsoft

Hi @Anonymous ,

The problem lies in the construct of your conditions - you can't string conditions by just stating the result of a condition.

For example;

If(firstName = "Bob" || "Sue" || "Frank")

won't work, but

If(firstname="Bob" || firstname="Sue" || firstname="Frank")

will work. 

Try this;

 

Coalesce(Parent.Error, If( "*"
in DataCardValue4.Text || "?" in DataCardValue4.Text || """" in DataCardValue4.Text, 
  "Please do not include any of these characters: "& Char(34)& " * : < > ? / \ |"))

 

 

 

If you're feeling adventurous, you can also use Match() or MatchAll() with a regex pattern to pick up special characters.  

 

Hope this helps,

 

RT

View solution in original post

Anonymous
Not applicable

Thank you! Appreciate the explanation!

Hi @Anonymous ,

Your post got me thinking there is a smarter way of doing this and it was something I might use myself, so I came up with this working model.

SpecialChar.gif

Firstly do a Collection of all special characters (you can do this at Screen OnVisible) by using their ASCII value ranges

ClearCollect(
   colChar,
   ForAll(
      Sequence(15),
      {FieldNo: Char(32 + Value)}
   ),
   ForAll(
      Sequence(7),
      {FieldNo: Char(57 + Value)}
   ),
   ForAll(
      Sequence(5),
      {FieldNo: Char(90 + Value)}
   ),
   ForAll(
      Sequence(4),
      {FieldNo: Char(122 + Value)}
   )
)

this puts 31 Special Characters into the collection under the field FieldNo.
Below is a gallery (with wrap at 4) showing the characters

WarrenBelz_1-1618640289481.png

Now put this on the OnChange of the Text Box

ForAll(
   colChar,
   If(
      FieldNo in Self.Text,
      Collect(
         colError,
         {CharError: true}
      );
      Reset(Self)
   )
)

and this on the OnSelect of the Text Box - you could also in addition put this at Screen OnVisible 

Clear(colError)

 Now put a Label (this is the warning message) on the screen with this as the Text

"Do not use characters " & Concat(
    colChar,
    FieldNo & ""
) & " here"

and this as the Visible

First(colError).CharError

NOTE - I had to use a Collection here as a Variable cannot be set inside a ForAll() statement.

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (1,237)