Only alphabet characters in GUI textfield

Hi Everybody,

In my game there is a GUI textfield where you can fill in your name.
What I would like to have is that you only can use the 26 characters from the alphabet (a … z and A … Z) and space.
I know that it’s not possible to disable keys from the keyboard but how can I achieve that nothing happens when another key is hit?

use following c# script

for header file:

using System.Text.RegularExpressions;
<yourtext>=Regex.Replace(<yourtext>, @"[a-bA-Z]", "");
1 Like

Thanks for reply.

I’m using javascript. Is there a way to translate it to javascript?

Is it possible to check what key is pressed every time when the player types a character of his name in the textfield?

The best thing is to intercept incoming characters and change them to null if they are undesired; see my answer here. (Unityscript uses the index of a string to indicate chars, so for example change ‘a’ in C# to “a”[0] in Unityscript).

–Eric

This is will help me.
Thanks for reply.

So I’m new to this but using the code above dindn’t give the results expected

string somString;
somString=Regex.Replace(somString, @"[a-bA-Z]", "*");//The line I Changed below
Debg.Log(somString)

input:!@#QWE123qwe
Console:!@#***123qwe

This had me scratching my head for at least half an hour, but i fixed it like this:

somString=Regex.Replace(somString, @"[^a-zA-Z]", "*");//Changed the "b" to an "z" and added the "^"

input:!@#QWE123qwe
Console:QWEqwe
Hope this helps

@YanrikLabCegep This is a 12 year old thread. Do not necro-post. Also your solution is wrong not only because the general approach is really old, but also because you’re just modifying a regular expression without properly understanding what it does. Please start a new thread if you need more help, but this topic alone isn’t really useful anymore (and mods will probably lock it now).