cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
mantastg08876
Responsive Resident
Responsive Resident

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 III
Super User III

Hi @mantastg08876 ,

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 @mantastg08876 ,

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 @mantastg08876 ,

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 III
Super User III

Hi @mantastg08876 ,

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 @mantastg08876 ,

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

Thank you! Appreciate the explanation!

Hi @mantastg08876 ,

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
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Don't miss the call this month on June 16th - 8a PDT

Top Solution Authors
Top Kudoed Authors
Users online (66,764)