I’m trying to create an input field that only accepts numbers, not symbols or letters. I was thinking I would just create a function that listened for when the field changed, try to parse each character in the string, and if I couldn’t, I’d get rid of them. That said, I know there’s no OnChanged function. Someone suggested using OnValidateInput, but I can’t seem to figure out how. Any help would be appreciated!
OnValidateInput is a delegate defined as “public delegate char OnValidateInput (string text, int charIndex, char addedChar);”. You’d do something like “myInputField.onValidateInput += MyValidationFunction;”. it expects you to return the char that is being added (‘’ for disallowing the input).
That being said for pure number input there is a Integer validation which makes sure that the input is of integer type which might be ok for your useage. The next beta will have the validation enum exposed in the inspector.
I’m trying to enable a button when there is text written to an input field, and disable the button when the input field is empty. I was using polling for the time being, but validation is better way. However, I noticed that deleting text with backspace or delete doesn’t go through the validation, so it is possible to type in text (button will be enabled), and then remove it and yet the button remains enabled. Should I raise a bug about this?
EDIT: In my case it would be best if input field had some kind of “value changed” event, as maybe that behaviour (deleting text doesn’t get validated) is by design?
yes this is by design, when you delete there is nothing to validate as your not breaking any possible character rules or length restrictions.
I’ll look at adding some sort of on change callback.
True, you’re right.
Thanks, that would be a great thing to have!
Lovely, thanks!
There isn’t ‘’ for chars in C#, so how do I disallow the input again?
Also, when I try to clean the input text and set it back in an Update, I get errors.
For example
string val = playerNameField.value;
val = val.Replace("-", "");
playerNameField.value = val;
results in an ArgumentOutOfRangeException for “startIndex” when I type the next character.
So what’s the proper way to alter the input text or is it even possible atm?
Sorry you could do “return (char)0”
That might be a bug. Could you submit a bug report for this.