[Quicktip] How to limit InputField to numeric values


Recently there was a question on Unity Answers about how to make an input field that only allows numeric values to be filled in. The solution is quite simple, especially if your making using of new UI stuff thats available since Unity-Version 4.6.

So let me show you how you can do it!

  1. Start by opening up your existing or a new Project file
  2. Insert an InputField into your scene
    Insert Input Field
    By executing this step a parent element called Canvas will be placed in the scene. If you have a look at the child object you should find an object named Input Field.
  3. Change the inspector type from Normal to Debug, that way we have access to more options of our textfield. (Right clicking on the inspector header should bring up the following menu)
    Change Unity Inspector type
  4. Scroll down to the Input Field Script. You should find there an option called Validation.
  5. Set it to float or int depending on the value that you wanted to be allowed to insert.Changing the Validation type
  6. Test it: Change to the Play-view and hit the Play button. If you now try to insert anything non numeric like (“a or b”). It shouldn’t appear in the input field.
    Final Input Field

Thats it for this Quicktip, I hope you did enjoy. Feel free to comment and share. 🙂

Cheers Alex

Advertisements

2 thoughts on “[Quicktip] How to limit InputField to numeric values

    1. Hi there,

      Thanks for your comment!

      You are absolutely right, it seems like it is no longer necessary to use this workaround.
      Therefore, I am planning on updating this post soon.

      Cheers.

      Alex

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s