Search Unity

TMP_InputValidation documentation

Discussion in 'UGUI & TextMesh Pro' started by Gillissie, Dec 17, 2019.

  1. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    305
    I can't find any info anywhere about how to actually implement the "Validate()" method of TMP_InputValidator.

    https://docs.unity3d.com/Packages/com.unity.textmeshpro@1.1/api/TMPro.TMP_InputValidator.html

    I created this class, but I don't know what to do with the input parameters "text" and "pos", and what to return if the character isn't valid?

    Code (CSharp):
    1.     public class BandNameInputValidator : TMP_InputValidator
    2.     {
    3.         public TextAsset validCharacters;
    4.  
    5.         public override char Validate(ref string text, ref int pos, char ch)
    6.         {
    7.            
    8.         }
    9.     }
     
    Harinezumi likes this.
  2. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    305
    This is what I ultimately decided to try, and I have confirmed that it's getting called, but even valid characters aren't showing up in my input field.

    Code (CSharp):
    1.     [CreateAssetMenu(menuName = "Gilligames/TMPro Validators/Band Name")]
    2.     public class BandNameInputValidator : TMP_InputValidator
    3.     {
    4.         public TextAsset validCharacters;
    5.  
    6.         public override char Validate(ref string text, ref int pos, char ch)
    7.         {
    8.             if (validCharacters.text.IndexOf(ch) > -1)
    9.             {
    10.                 return ch;
    11.             }
    12.  
    13.             return '\0';
    14.         }
    15.     }
     
    wdr434-domowe likes this.
  3. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    305
    The input value "text" is always empty string, which is why nothing is showing up in the input field.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    Here is a revised example custom validator.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. namespace TMPro
    5. {
    6.     /// <summary>
    7.     /// EXample of a Custom Character Input Validator to only allow digits from 0 to 9.
    8.     /// </summary>
    9.     [Serializable]
    10.     [CreateAssetMenu(fileName = "InputValidator - Digits.asset", menuName = "TextMeshPro/Input Validators/Digits", order = 100)]
    11.     public class TMP_DigitValidator : TMP_InputValidator
    12.     {
    13.         // Custom text input validation function
    14.         public override char Validate(ref string text, ref int pos, char ch)
    15.         {
    16.             if (ch >= '0' && ch <= '9')
    17.             {
    18.                 text += ch;
    19.                 pos += 1;
    20.                 return ch;
    21.             }
    22.             return (char)0;
    23.         }
    24.     }
    25. }
    26.  
    The line text += ch; was missing.

    The custom validator gets the ref text in order to populate / manipulate it.

    P.S. This feature is / was experimental. However, we can certainly evolve it as you work on this.
     
    sp-LeventeLajtai likes this.
  5. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    305
    Thanks for the reply, Stephan. It would be immensely helpful to have that information in the documentation page that I linked to above, if possible.
     
  6. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    305
    You may have noticed in my code above that I am linking to a TextAsset that contains all the valid characters for the input field. I think it would be nice if TextMeshPro had a standard validation option that simply lets you link a TextAsset to the inspector, and it only allows input of the characters found in the text asset. This would be extremely flexible and re-usable without the need to create a new validator just to specify certain valid characters.

    I use the same TextAsset to define fonts as I do for the validation of input fields, since I then know that only characters included in the font will be allowed for input.
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    Seems like that would be useful functionality. I'll take a look at this when I have time.
     
  8. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,633
    A couple of things:
    This example doesn't account for the fact that a character may be inserted into the text

    Appending with:
    Code (csharp):
    1. text += ch;
    Needs to be changed to something like:
    Code (csharp):
    1. text = text.Insert(pos, ch.ToString());
    Second, while the above fixes insertion of text, when some text is highlighted and you press a character, the selection is not cleared, but instead the new character is inserted at the beginning of the selection.If the entire string is highlighted, the character is added to the end. As far as I've been able to figure out, there's no way to properly support text selection replacement if you are using a custom validator because there is no way to access the information about what is currently selected.

    Tested in 1.5.0 preview 4.
     
    Gillissie and sp-LeventeLajtai like this.
  9. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    Hi,
    could you please add this to the Unity Manual and Scripting reference, please?
    Thanks!
     
    cfazilleau and sp-LeventeLajtai like this.
  10. Zapan15

    Zapan15

    Joined:
    Apr 11, 2011
    Posts:
    186
    Please find attached a version which allows writing a decimal point or comma (e.g. used in germany):

    Code (CSharp):
    1.         public override char Validate(ref string text, ref int pos, char ch)
    2.         {
    3.             //Allow handling . or , for float input
    4.             bool ok = false;
    5.             if (char.IsNumber(ch)) {
    6.                 text = text.Insert(pos, ch.ToString());
    7.                 ok = true;
    8.             } else {
    9.                 if ((ch == '.') || (ch == ',')) {
    10.                     ok = (!text.Contains(".")) && (!text.Contains(","));
    11.                     if (ok) {
    12.                         text = text.Insert(pos, ch.ToString());
    13.                     }
    14.                 }
    15.             }
    16.  
    17.             if (ok) {
    18.                 pos++;
    19.                 return (ch);
    20.             } else {
    21.                 return '\0';
    22.             }
    23.         }  
     
    JariHuomo and Stephan_B like this.
  11. sp-LeventeLajtai

    sp-LeventeLajtai

    Joined:
    Jul 11, 2019
    Posts:
    10
    Could you please add this example to the manual and scripting page? It is sorely missing example code (as well as parameter and return value description)!
     
    cfazilleau and Gillissie like this.
  12. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    305
    Stephan, could you please address the reply that guavaman made above? I came here to write the exact same thing just now. Thanks.
     
    cfazilleau likes this.
  13. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    I'll try taking a look later today.
     
  14. cfazilleau

    cfazilleau

    Joined:
    Apr 10, 2019
    Posts:
    6
    The documentation still misses this information more than two years after and for the latest version.
     
  15. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    Bump!

    I came here because the Regex validator (built-in) doesn't allow me to use backslashes it seems.. at least it breaks when I do, and then starts allowing no characters whatsoever.

    I wanted a (sort of) simple Regex pattern,
    \(?[0-9\.]*,\s*[0-9\.]*\)?

    Looks complicated, but I just want to support latitude longitude coordinates, which could be written out like this (and these match the pattern given above):
    (40.4, -78)
    39.5, -80.1
    (37,-83)

    So I looked up how to make a custom validator, and well.. it seems the docs are still missing this info?
     
    Atomiz2002 likes this.
  16. Avelblood

    Avelblood

    Joined:
    May 5, 2016
    Posts:
    6

    Nothing is ever being done. Forget about it.
     
    ModLunar and Atomiz2002 like this.
  17. Atomiz2002

    Atomiz2002

    Joined:
    Feb 2, 2020
    Posts:
    14
    @Stephan_B Better later than never. Also consider the automatic TMP validation based on the text atlas asset, this is a must-have feature.

    Edit: Just noticed the Regex validation feature under the Input Field -> Content Type -> Character Validation...Not sure how i missed that the first time and jumped to the custom validator
     
    Last edited: Feb 15, 2024
    ModLunar likes this.
  18. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    So true....

    Yeah, I just stopped using Unity and started making my own cross-platform C++/C# game engine since Sep 13, 2023.
    Even with the challenges, for me, it's so much better.