InputField OnValueChange

Ok, the OnValueChange, as I was searching about it, seems to still have many flaws as not seeing Ctrl+Vs, Ctrl+X, backspaces, it doesn’t seem to actually check every condition, but ok for now. My problem is that I can’t seem to mask format the data as the user writes it.

Took me about a day looking for answers to find out that every single one I found telling me to change the text was wrong, that I had to change the text from InputField. It works on onEndEdit, but doesn’t work on onValueChanged.

So I wanted to know if anyone know a workaround this.

Well, for those who want to know what I am doing:

public void FormatTel(UnityEngine.UI.InputField IFD_Telefone)
{
       IFD_Telefone = FormatWithMask(IFD_Telefone.text, "(##) ####-####");
}

public string FormatWithMask(string input, string mask)
    {
        if (string.IsNullOrEmpty(input)) return input;
        var output = string.Empty;
        var index = 0;
        foreach (var m in mask)
        {
            if (m == '#')
            {
                if (index < input.Length)
                {
                    output += input[index];
                    index++;
                }
            }
            else
                output += m;
        }
        return output;
    }

I call the FormatTel from the onValueChanged from IFD_Telefone. That gives me a Null Reference Exception, need a workaround to change the values for that format shown above.

The on valuechanged sends a string not a InputField. so unless you have more code what you’ve posted i dont think would work.

The code is only that.

On the inspector, inside the onValueChanged method I choose the function FormatTel, and sent the InputField object to that method call, since changing the string I would receive would not change the original and I had no way, without a reference of the object, to change the original text.

As I see, when I was doing the changes for testing and sent the code here, I forgot to put the .text in the beggining, as you noticed. But that doesn’t solve the issue either.

Here is the example project I made of the issue:

1929552–124669–Demo.rar (84.4 KB)