Input Field and character limit bug

Im using an input field and setting the text to some value which is not bigger than the character limit (which is 20).
and an exception is being thrown:
ArgumentOutOfRangeException: startIndex + length > this.length

By looking at the source of the input field im seeing where this is comming from:

        public string text
        {
            get
            {
                return this.m_Text;
            }
            set
            {
                if (this.text == value)
                    return;
                if (this.onValidateInput != null || this.characterValidation != InputField.CharacterValidation.None)
                {
                    this.m_Text = string.Empty;
                    InputField.OnValidateInput onValidateInput = this.onValidateInput ?? new InputField.OnValidateInput(this.Validate);
                    int num = this.characterLimit <= 0 ? value.Length : Math.Min(this.characterLimit - 1, value.Length);
                    for (int index = 0; index < num; ++index)
                    {
                        char ch = onValidateInput(this.m_Text, this.caretPositionInternal, value[index]);
                        if ((int)ch != 0)
                            this.m_Text += (string)(object)ch;
                    }
                }
                else
                    this.m_Text = this.characterLimit <= 0 ? value : value.Substring(0, this.characterLimit);
                if (!Application.isPlaying)
                {
                    this.SendOnValueChangedAndUpdateLabel();
                }
                else
                {
                    if (this.m_Keyboard != null)
                        this.m_Keyboard.text = this.m_Text;
                    if (this.m_CaretPosition > this.m_Text.Length)
                        this.m_CaretPosition = this.m_CaretSelectPosition = this.m_Text.Length;
                    this.SendOnValueChangedAndUpdateLabel();
                }
            }
        }

This is happening because of this line:

this.m_Text = this.characterLimit <= 0 ? value : value.Substring(0, this.characterLimit);

It is happening only when the validation is empty and when the character limit is greater than 0.

1 Like

Thanks for this report. This bug was fixed, but it doesn’t look like the fix was grafted into the 5.4 beta. We’ll try to get this into the next update.

1 Like

Thank you!