[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…

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

onValidInput beta 21 documentation

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;
 }