- Home /
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 ?
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...
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.
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.
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