Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Bug TMP_InputField character validation

Discussion in 'UGUI & TextMesh Pro' started by Nineteendo, Sep 9, 2023.

  1. Nineteendo

    Nineteendo

    Joined:
    Aug 8, 2022
    Posts:
    1
    I found two bugs related to character validation in TMP_InputField:
    1. Pasting from the clipboard
    TMP_InputField.Append(string input) doesn't allow \v & doesn't prevent latin-1 supplement.
    2. Typing new characters:
    TMP_InputField.IsValidChar doesn't prevent control characters.

    This can be easily solved:
    Code (CSharp):
    1. protected virtual bool IsValidChar(char c)
    2. {
    3.     // TODO - Fix bugs with text starting with \u0003
    4.     return c >= '\t' && c <= '\v' || c == '\r' || c >= ' ' && c <= '~' || c >= '\u00A0';  // Allows only printable characters
    5.    // With the addition of Dynamic support, I think this will best be handled by the text component.
    6.    //return m_TextComponent.font.HasCharacter(c, true);
    7. }
    8.  
    9. protected virtual void Append(string input)
    10. {
    11.     if (readOnly || !InPlaceEditing())
    12.         return;
    13.  
    14.     for (int i = 0, imax = input.Length; i < imax; ++i)
    15.     {
    16.         char c = input[i];
    17.         if (IsValidChar(c))  // More concise
    18.             Append(c);
    19.     }
    20. }
    It's possible to fix this in a child class of TMP_InputField, but that requires to re-implement InPlaceEditing as it's private.