cancel
Showing results for 
Search instead for 
Did you mean: 
WarrenBelz

Prevent user from entering Special Characters in a Text input

This question came from a post I responded to and got me thinking that this was something that may be required at times, so I came up with the model in this blog. It may have other application as well as the Text Box example.

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. There may be some you want to allow such as underscore _ (95) so you would have to adjust the below to suit.
Below is a gallery (with wrap at 4) showing the characters

WarrenBelz_0-1618641343827.png

Now put this on the OnChange of the Text Box

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

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

Put 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

 

Comments

I like that. Thanks for sharing!

 

In my cases, I would modify it to not reset the text input control. 😉

Excellent article!

Meet Our Blog Authors
  • PowerApps Community Mentor 2018, Practicing surgeon and former Professor of Surgery, University of Illinois, Chicago.
  • Jill of All Trades (Power Apps, SharePoint, PowerShell, Exchange, Power Automate and odds & ends of assorted other things)
  • I am the Owner/Principal Architect at Don't Pa..Panic Consulting. I've been working in the information technology industry for over 30 years, and have played key roles in several enterprise SharePoint architectural design review, Intranet deployment, application development, and migration projects. I've been a Microsoft Most Valuable Professional (MVP) 12 consecutive years and am also a Microsoft Certified SharePoint Masters (MCSM) since 2013.
  • I'm Tim Leung - a PowerApps builder. In addition to PowerApps, my main interests include building software with SQL Server and Microsoft .NET. I'm the author of 'Beginning PowerApps' - the first published book on PowerApps. You can buy this from Amazon and other leading bookstores.
  • Systems developer
  • My name is Timothy Shaw and I create digital solutions using the Power Platform, Office 365, and Azure SQL and handle the IT for a small company in the energy sector. When not building the future :), I enjoy playing guitar, good (or really bad!) sci-fi, Xbox therapy, and hanging with my wife and son. Twitter: @ShortForTim