Meetes

Custom TMP_InputValidator

Apr 13th, 2021
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.94 KB | None | 0 0
  1. /// <summary>
  2. /// Create your validator class and inherit TMPro.TMP_InputValidator
  3. /// Note that this is a ScriptableObject, so you'll have to create an instance of it via the Assets -> Create -> Input Field Validator
  4. /// </summary>
  5. [CreateAssetMenu(fileName = "Input Field Validator", menuName = "Input Field Validator")]
  6. public class CustomValidator : TMPro.TMP_InputValidator
  7. {
  8.     /// <summary>
  9.     /// Override Validate method to implement your own validation
  10.     /// </summary>
  11.     /// <param name="text">This is a reference pointer to the actual text in the input field; changes made to this text argument will also result in changes made to text shown in the input field</param>
  12.     /// <param name="pos">This is a reference pointer to the input field's text insertion index position (your blinking caret cursor); changing this value will also change the index of the input field's insertion position</param>
  13.     /// <param name="ch">This is the character being typed into the input field</param>
  14.     /// <returns>Return the character you'd allow into </returns>
  15.     public override char Validate(ref string text, ref int pos, char ch)
  16.     {
  17.         Debug.Log($"Text = {text}; pos = {pos}; chr = {ch}");
  18.         // If the typed character is a number, insert it into the text argument at the text insertion position (pos argument)
  19.         if (char.IsNumber(ch) && text.Length < 14)
  20.         {
  21.             // Insert the character at the given position if we're working in the Unity Editor
  22. #if UNITY_EDITOR
  23.             text = text.Insert(pos, ch.ToString());
  24. #endif
  25.             // Increment the insertion point by 1
  26.             pos++;
  27.             // Convert the text to a number
  28.             long number = ConvertToInt(text);
  29.             // If the number is greater than 9999999999 / 11 characters or longer
  30.             // Then reparse the number string but trimmed to 10 characters
  31.             if(number> 9999999999)
  32.             {
  33.                 number = long.Parse(number.ToString().Substring(0, 10));
  34.             }
  35.  
  36.             // Format the string incrementally by the character count of our number
  37.             // Format: (XXX) XXX-XXXX
  38.             switch (number.ToString().Length)
  39.             {
  40.                 case 1:
  41.                     text = System.String.Format("{0:(#}", number);
  42.                     break;
  43.                 case 2:
  44.                     text = System.String.Format("{0:(##}", number);
  45.                     break;
  46.                 case 3:
  47.                     text = System.String.Format("{0:(###) }", number);
  48.                     break;
  49.                 case 4:
  50.                     text = System.String.Format("{0:(###) #}", number);
  51.                     break;
  52.                 case 5:
  53.                     text = System.String.Format("{0:(###) ##}", number);
  54.                     break;
  55.                 case 6:
  56.                     text = System.String.Format("{0:(###) ###-}", number);
  57.                     break;
  58.                 case 7:
  59.                     text = System.String.Format("{0:(###) ###-#}", number);
  60.                     break;
  61.                 case 8:
  62.                     text = System.String.Format("{0:(###) ###-##}", number);
  63.                     break;
  64.                 case 9:
  65.                     text = System.String.Format("{0:(###) ###-###}", number);
  66.                     break;
  67.                 case 10:
  68.                     text = System.String.Format("{0:(###) ###-####}", number);
  69.                     break;
  70.                 default:
  71.  
  72.                     return '\0';
  73.             }
  74.  
  75.  
  76.             // Increment the text insertion position by 1 in the following positions
  77.             // (
  78.             // (XXX)_XXX-
  79.             if (pos==1 || pos == 9)
  80.             {
  81.                 pos++;
  82.             }
  83.             // Increment the text insertion position by 2 in the following positions
  84.             // (XXX)
  85.             // (XXX)_
  86.             else if (pos == 4 || pos == 5)
  87.             {
  88.                 pos += 2;
  89.             }
  90.             return ch;
  91.            
  92.         }
  93.         // If the character is not a number, return null
  94.         else
  95.         {
  96.             return '\0';
  97.         }
  98.     }
  99.  
  100.     /// <summary>
  101.     /// Converts a string input into a long integer
  102.     /// </summary>
  103.     /// <param name="inputText">Input text</param>
  104.     /// <returns>Returns the number of the text</returns>
  105.     long ConvertToInt(string inputText)
  106.     {
  107.         // Create a string builder to cache our number  characters
  108.         System.Text.StringBuilder number = new System.Text.StringBuilder();
  109.         // Iterate through each character in the input text
  110.         // If the character found is a digit, append the digit to our string builder
  111.         foreach (char character in inputText)
  112.         {
  113.             if (char.IsDigit(character))
  114.             {
  115.                 number.Append(character);
  116.             }
  117.         }
  118.         // Return the numbered string
  119.         return long.Parse(number.ToString());
  120.     }
  121. }
Add Comment
Please, Sign In to add comment