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.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

TMP_InputValidation documentation

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

  1. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    276
    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:
    276
    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.     }
     
  3. Gillissie

    Gillissie

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

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,525
    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:
    276
    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:
    276
    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

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,525
    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,192
    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!
     
    Cfaz and sp-LeventeLajtai like this.
  10. Zapan15

    Zapan15

    Joined:
    Apr 11, 2011
    Posts:
    156
    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.         }  
     
    Stephan_B likes this.
  11. sp-LeventeLajtai

    sp-LeventeLajtai

    Joined:
    Jul 11, 2019
    Posts:
    8
    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)!
     
    Cfaz and Gillissie like this.
  12. Gillissie

    Gillissie

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

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,525
    I'll try taking a look later today.
     
    Cfaz, sp-LeventeLajtai and Gillissie like this.
  14. Cfaz

    Cfaz

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