Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question TMP_Inputfield block delete and backspace

Discussion in 'UGUI & TextMesh Pro' started by robal1991, Jan 10, 2024.

  1. robal1991

    robal1991

    Joined:
    Mar 31, 2016
    Posts:
    33
    Hello,
    I'm doing a time picker in text mesh pro inputfield.
    The format is 24H with leading 0, examples:
    * 10:15
    * 12:00
    * 00:00
    * 23:59

    I've done a custom input validator that handles replacing the digits and keeping the colon.

    But when I press backspace or delete the change does not go through my validator and I cannot block it. How can I achieve that?
    My code:

    Code (CSharp):
    1.  
    2.     public class HourInputfieldValidator : TMP_InputValidator
    3.     {
    4.         public override char Validate(ref string text, ref int pos, char ch)
    5.         {
    6.             Debug.Log("Validate: " + text + " pos: " + pos + " ch: " + ch + "   character int: " + (int)ch);
    7.  
    8.             if (text.Length == 5)
    9.             {
    10.                 if ((ch >= '0' && ch <= '9') || ch == ':')
    11.                 {
    12.                     string backup = text;
    13.  
    14.                     if (pos <= 4)
    15.                     {
    16.                         if (pos == 2)
    17.                         {
    18.                             pos = 3; //go after colon
    19.                         }
    20.  
    21.                         text = text.Remove(pos, 1);
    22.                         text = text.Insert(pos, ch.ToString());
    23.  
    24.                         if (pos == 0 && ch == '2' && text[1] > '3') //when changing from 16:XX to 26:XX make it a valid 23:XX
    25.                         {
    26.                             text = text.Remove(1, 1);
    27.                             text = text.Insert(1, "3");
    28.                         }
    29.  
    30.                         Debug.Log("Text before regex: " + text);
    31.  
    32.                         const string regex = "(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]"; //"HH-mm" format with leading 0
    33.  
    34.                         Match match = Regex.Match(text, regex);
    35.                         if (match.Success)
    36.                         {
    37.                             Debug.Log("Match success!");
    38.                             pos++;
    39.                             return ch;
    40.                         }
    41.                         else
    42.                         {
    43.                             Debug.Log("Match failed. Restore from backup: " + backup);
    44.                             text = backup;
    45.                         }
    46.                     }
    47.                 }
    48.             }
    49.             return (char)0;
    50.         }
    51.     }
    The Validate method is not called for Delete and Backspace characters.

    Another big problem is that on mobile device it is not working at all. An additional input textbox is shown above the keyboard and it does not even call my validate method when adding new characters.
     
    Last edited: Jan 11, 2024