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!

Thank you very much for this! 

Thank you so much for sharing this. Love it!

Hi @ALL ,

 

cool article, but i have a problem within the If() statement i get an error for the fasle value:

Invalid argument type (boolean). A table value is expected instead.

 

zlex_1-1658221897062.png

 

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

without Reset(Self) it is working.

(i have to use ";" instead of "," , because of german region settings)

 

Any ideas?

 

Thanks

You must not have it on the OnChange of the Text box, otherwise Reset(Self) would be valid.

Hello WoarrenBelz,

 

but you wrote to put this on the OnChange Event of the Text box?

zlex_0-1658730986838.png

So, i think i don't understand you. Sorry 😁.

Might be lost in translation here -  I was saying you will not get an error if it is on the OnChange. If you still cannot solve this. please post a new thread on the forum as this is not really a discussion area.

Hi @WarrenBelz ,

 

Great tutorial and its probably just me but I'm struggling to narrow the special character limit to just double-quotes. I looked up the ASCII code for double-quotes and its 34, but when I use the below shortened OnVisible function it blocks only # from being used. # is code 35 by the same table. ASCII must not be what is used, but I'm not sure what codes are in use here.

 

Do you have a link to the special character dictionary that this function uses? Am I doing something wrong with this function?

 

ClearCollect(
   colChar,
   ForAll(
      Sequence(1),
      {FieldNo: Char(34 + Value)}
   )
)

 

 

 

Hi @Ginko ,

If you just want the one character " excluded

If(
    Char(34) in Self.Text,
    Reset(Self)
)

 

Thank you @WarrenBelz, I have your process amended slightly to apply to all the fields of a form, with one label that is made visible and hides the submit button if a non-numeric character (other than ".") is inputted in any field.  Specifically to force the user to not put currency symbols or "%" characters in.

 

[UPDATE] IT WORKS PERFECTLY

 

Hi @WarrenBelz 

 

Thank you very much for your solution.

 

1 issue that I'm facing is that, If I type special characters in the text input in the preview mode(F5)

 

The error does not appear and text input doesn't reset

 

However, the moment I click "X" and exit preview mode, the error message appears and the input resets.