Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by fingerbib · Oct 26, 2014 at 05:29 PM · 4.6

[New UI 4.6] InputField - validate input to only permit positive integers.

I am using an InputField component, and I wish to restrict the input to be only positive integers.

I can use inputField.validation = InputField.Validation.Integer; to restrict input to integers, but how can I extend this to allow only positive integers?

Thanks in advance...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Landern · Oct 26, 2014 at 05:30 PM

Use onValidateInput and ensure the input is not a string and only positive numbers:

onValidInput beta 21 documentation

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fingerbib · Oct 28, 2014 at 09:37 PM 0
Share

Thanks for the quick reply, Landern, but there's no actual documentation there except the function name. How does one go about using it? I see in my code that the function defintion is:

 public delegate char OnValidateInput(string text, int charIndex, char addedChar);

what is expected in the input params? I'm not sure how to use this...

avatar image Kiwasi · Oct 28, 2014 at 09:52 PM 0
Share

I believe you would handle this by simply declaring a function that matches the signature. Same as OnCollisionEnter or Update.

I could be wrong.

avatar image fingerbib · Oct 28, 2014 at 10:25 PM 1
Share

thanks, I understand what you mean about matching the signature, I was able to use

     void Start()
     {
         InputField inputField = gameObject.GetComponent<InputField>();
         inputField.validation = InputField.Validation.Integer;
 
         inputField.onValidateInput += ValidateInput;
 
     }
 
     public char ValidateInput(string text, int charIndex, char addedChar)
     {
         char temp = 'c';
         return temp;
     }


I still don't understand what the char return value signifies though...

In any case, my inputFields are not even accepting any input since I just updated to the 21 beta, so I need to figure out what's going wrong there before I can continue with validation. $$anonymous$$any thanks for your help.

avatar image
0

Answer by fingerbib · Oct 29, 2014 at 09:42 PM

Here is my own solution, it's a bit clunky, but using the OnValidation delegate seems to override any inbuilt validation (i.e. InputField.validation no longer screens for non ints)

void Start() { inputField = gameObject.GetComponent(); inputField.validation = InputField.Validation.Integer;

  inputField.onValidateInput += ValidateInput;
 

}

public char ValidateInput(string text, int charIndex, char addedChar) { char output = addedChar;

      if (addedChar != '1'
          && addedChar != '2'
          && addedChar != '3'
          && addedChar != '4'
          && addedChar != '5'
          && addedChar != '6'
          && addedChar != '7'
          && addedChar != '8'
          && addedChar != '9'
          && addedChar != '0')
      {
          //return a null character
          output = '\0';
      }
 
      return output;
  }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SimRuJ · Sep 27, 2018 at 12:27 PM 0
Share

There's a shorter way ;):

 output = Regex.Replace(addedChar+"",@"[0-9]","\0"); //"output" is a String
 return output.ToCharArray()[0];

This doesn't let you type 0 to 9. If you only want to allow 0-9 but nothing else, you can use:

 "[^0-9]"

Don't forget to add:

 using System.Text.RegularExpressions;

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[4.6 UI] Image fading - How to? 2 Answers

How do I add a mesh to a button object in 4.6b? 0 Answers

[4.6 - UI] Accessing Text in Image inside Canvas via C# Script 3 Answers

[4.6 - UI] Keep aspect ratio 2 Answers

4.6 play animation on game over 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges