Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by hobblygobbly · May 04, 2015 at 12:12 PM · uiinputfieldvaluelistener

Force uppercase in Input Field without using onValueChange?

I wish to restrict input fields entry to all caps. However,



   void Awake ()
   {
     inputField = GetComponent<InputField>();
     inputField.onValueChange.AddListener( delegate { Manage(); } );
   }

   void Manage ()
   {
     inputField.text = inputField.text.ToUpper();
     Debug.Log( "Test" );
   }




The problem obviously is an infinite loop.



What's the best way to restrict input entry in Unity3D UI InputField ?



Comment
Add comment · Show 1
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 sethuraj · May 04, 2015 at 12:04 PM 0
Share

Cant you make the text to upper case after user puts the input..?I haven't started using unity GUI much.But cant you find a way to trigger an event when the input field loses focus(player clicks outside the input field) change it to upper case...

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by rectdev · Jan 31, 2019 at 09:05 PM

I know this is an old question but I came across with it today for an answer, and I found an alternative that works just as well.

You can add an input verification function to the input field which replaces and/or eliminate characters on the go.

For the game I'm writing I needed names with only minuscule letters, numbers, space and dash. Also the alphanumeric verification didn't work reliable enough on latin-extended input, so I added this piece of code:

this goes to class variables:

 public InputField inputField;

this goes to start function:

  inputField.onValidateInput += delegate (string input, int charIndex, char addedChar) {
        return nameValidation(addedChar);
  };

and this is the addedChar function that does the control and modification on per character basis:

 private char nameValidation(char c)
 {
     if (c >= 'A' && c <= 'Z') {
         return (char)((int)c - 'A' + 'a');
     }
     else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == ' ') {
         return c;
     } else {
         return '\0';
     }
 }


no more onChange causing more change!

And here's an even simpler function to force uppercase...



 void Start()
 {
     your_input.onValidateInput += 
         delegate (string s, int i, char c) { return char.ToUpper(c); };
 }
 



And if you want ONLY letters:

 void Start()
 {
     your_input.onValidateInput += delegate (string s, int i, char c) { return char.ToUpper(c); };
 }
 
 char Val(char c)
 {
     c =  char.ToUpper(c);
     return char.IsLetter(c) ? c : '\0';
 }



Here's uppercase letters only, and a length limit:



     your_input.onValidateInput += delegate (string s, int i, char c)
     {
         if (s.Length >= 4) { return '\0'; }
         c = char.ToUpper(c);
         return char.IsLetter(c) ? c : '\0';
     };




(Alternately, don't forget the very handy .CharacterValidation approach. It is often all you need:)



 void Start()
 {
     your_input.characterValidation = InputField.CharacterValidation.Alphanumeric;
 }



So when using .onValidateInput the .Net "char." functions are very handy.



Comment
Add comment · 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
2

Answer by AeonIxion · May 04, 2015 at 12:39 PM

So the problem is that it keeps looping. You could save the inputfield.text into a variable and then compare it to inputField.text.ToUpper(). If it's the same, then don't do anything.

 string text = inputField.text;
 if(text != inputField.text.ToUpper())
 {
         inputField.text = inputField.text.ToUpper();
 }

I think this will work.

Comment
Add comment · 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
1

Answer by thelackey3326 · Nov 04, 2015 at 06:30 PM

You can set a validation callback for the input field, and if the character is valid, return its uppercase form.

There is a full example fortunately in the Unity manual:

http://docs.unity3d.com/ScriptReference/UI.InputField-onValidateInput.html

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

InputField instance isFocused always return false. 0 Answers

Pass in a reference directly into a script so that it update the appropriate variable value when the slider is moved. 0 Answers

How do i check the inputted text in a Multi line Input field? 1 Answer

InputField's OnEndEditting: Triggered when input (click, touch, etc) on other area rather than InputField. 0 Answers

UI InputField Undo Redo 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