Categories
Unity

Input Manager Validation

So, I am working on a project where I have an input field set up. We are asking for a first name, and thankfully it hit me at one point (in a moment of panic) that I have to prevent people from accidently adding a space, number or punctuation! You see, in the project, the first letter of the name is used later on to create a talisman. So if someone didn’t put in a letter, then well, that would mess things up LOL. I thought I was going to have to do some regex magic, and that’s what I saw in a bunch of message board answers. However the answers was WAY simpler.

TMP_InputField.ContentType.Name;
ForceLabelUpdate();

You can change the content type of the input box!! And thankfully there was one for name that doesn’t allow spaces or punctuation at the begninning (you can add a space and/or apostrophe later), or numbers. And those two lines of code above made it happen! Apparently the “ForcedLabedUpdate” just enforces that change.

These were the full two lines of code that I added to Start. My inputfield was called messagebox.

    messagebox.contentType = TMP_InputField.ContentType.Name;
    messagebox.ForceLabelUpdate();

And that was that!