Character Restriction in TextArea?

Ok, so I have a TextArea setup for a player to enter their name, but I want to restrict the characters they can use to standard alphabetical characters (ex - abcdefghijklmnopqrstuvwxyzABCDE…etc etc) and not let them enter stuff like “(.)/”

Is there a way to limit which characters can be typed in a textArea? Or will have I have to a separate check afterwards, so after I update the varaible, I manually go thru and do a check on the data and update it again?

Well, remember that GUI controls aren’t objects but functions that get called every frame. So every frame you’ll have the string as currently typed by the user so you can just check that each time and remove any bad chars:

user_input = GUI.TextArea(Rect(..), user_input)
user_input = user_input.Replace(illegal_chars, blank_space)

(note: very pseudo code :slight_smile:

Cool cool. Yeah, that’s what I figured I would have to do. Good to know. Thanks for the info Timmer!

Another method (which I use) is to check the keyboard input, and replace any unwanted chars with null. This way they never have a chance to end up in the string in the first place.

–Eric

Thanks for the suggestions guys. I decided to go a route similar to what Timmer was suggesting. I decided to use Regular Expressions, as it is very flexible and only 1 line of code. For anyone else who may stumble on this thread, this is the solution I came up with:

// Top of code
import System.Text.RegularExpressions;
var playerName = "";

funcion OnGUI()
{
// Add a name input field
playerName = GUI.TextArea(Rect(10,10,200,20), playerName, 15);

// Remove unwanted characters from playerName
playerName = Regex.Replace(playerName, "[^a-zA-Z0-9 ]", "");
}

Note the space after the 9 in “[^a-zA-Z0-9 ]”. So this code will only allow letters, numbers and spaces. Hope this helps someone.