This is straight forward. I have an input field and I want to restrict the user of inputting any spaces. I was thinking to make a script that checks if the user typed in a space and then remove it, but is there a simpler way of doing this?

  • \$\begingroup\$ Change the font of that input field, don't include the space ASCII character code in the font import file. So that means setting the offset to 33. That should be it for not letting the user see space inputs. Of course, they will still be inputting spaces (just not seeing them) but you can filter them out when they send the string for whatever you need it for. I don't really have time to test this right now but if you do let me know and I'll do the same if I test it. \$\endgroup\$ – John Hamilton Dec 31 '16 at 21:08
  • \$\begingroup\$ @Leon Laci : Have you even tried to make some research ? gamedev.stackexchange.com/questions/134541/… \$\endgroup\$ – Hellium Dec 31 '16 at 22:35
  • \$\begingroup\$ I'm still new to unity and c# so I don't understand most of the code and how to use it in my case, but I will try to make it work for me. \$\endgroup\$ – user95747 Jan 1 '17 at 12:42

There is an easy solution IF you only require alphanumeric characters (alphanumeric characters include: a-z,A-Z,0-9). In the input field, change the content type to alphanumeric. This will only allow letters and numbers, not spaces.

content type

You can also try out the other options too. Otherwise, if you require symbols too, you will have to use a script with a checking function that removes spaces. This would be initiated by the On Value Changed, which you can also se in the image.

I can quickly code an example script if you want me to.

EDIT: OK, I have made an example script and will show you how to use On Value Changed. Start by making a new c# script called 'UpdateInputField' and replace the text with this:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;// this is needed to use the 'InputField' 

public class UpdateInputField : MonoBehaviour
    InputField IF;
    void Start()
        IF = GetComponent<InputField>();//get the input field
    public void UpdateField()
        string text = IF.text;//get text from input field
        text=text.Replace(" ","");//fliter spaces from text
        IF.text = text;//set the text in the input field to the filtered text

Go and add this script to the input field object. Next, click on the + sign on 'On Value Changed': Then click on the bottom left box. In the window that appears, find the input field object in your scene and select it.


After that, click on the top right box and select UpdateInputField, UpdateField()

enter image description here

And that should be it! The OnValueChanged part automatically calls the UpdateField() function in the script whenever the input field is typed into. The UpdateField() function will then filter any spaces from the text and then set the text in the input field to the filtered text.

| improve this answer | |
  • \$\begingroup\$ I don't know how the on value change works, I was first thinking making a update method that checks if there are empty spaces. \$\endgroup\$ – user95747 Jan 2 '17 at 10:47
  • 1
    \$\begingroup\$ @Gnemlock I have updated my answer with an example \$\endgroup\$ – Joe Speers Jan 2 '17 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy