Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Date input field, adding char issue

Discussion in 'Unity UI & TextMesh Pro' started by -Aymeric-, Nov 13, 2017.

  1. -Aymeric-

    -Aymeric-

    Joined:
    Oct 21, 2014
    Posts:
    95
    Hello,

    I'm trying to create an input filed with a birth date format: dd/mm/yyyy.

    Here is my code (note I'm not checking if the date is correct: ex. 30 february etc. yet):
    Code (csharp):
    1. using TMPro;
    2. using UnityEngine;
    3.  
    4. public class BirthDateFormat : MonoBehaviour {
    5.  
    6.     public TMP_InputField input;
    7.  
    8.     bool allSlashAdded;
    9.  
    10.     void Start()
    11.     {
    12.         input.onValidateInput += OnValidateInput;
    13.     }
    14.  
    15.     char OnValidateInput(string text, int charIndex, char addedChar)
    16.     {
    17.         Debug.Log("text: " + text + ", charIndex: " + charIndex + ", addedChar: " + addedChar);
    18.      
    19.         if (!allSlashAdded && (input.text.Length == 1 || input.text.Length == 4))
    20.         {
    21.             allSlashAdded = input.text.Length == 5;
    22.  
    23.             input.text = text + addedChar + "/";
    24.  
    25.             input.stringPosition = input.text.Length;
    26.  
    27.             Debug.Log("/ added");
    28.  
    29.             return '\0';
    30.         }
    31.  
    32.         Debug.Log(addedChar + " is digit : " + char.IsDigit(addedChar));
    33.  
    34.         return char.IsDigit(addedChar) ? addedChar : '\0';
    35.     }
    36.  
    37.     void OnDestroy()
    38.     {
    39.         input.onValidateInput -= OnValidateInput;
    40.     }
    41. }

    It works fine in Unity, but on iOS after the first "/" added, I can't write more (see logs below, it's very strange)! Any ideas? Is it the correct way to add char and move the caret (stringPosition was tricky)!
    Code (csharp):
    1. text: , charIndex: 0, addedChar: 1
    2. BirthDateFormat:OnValidateInput(String, Int32, Char)
    3. TMPro.TMP_InputField:LateUpdate()
    4. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    5.  
    6. 1 is digit : True
    7. BirthDateFormat:OnValidateInput(String, Int32, Char)
    8. TMPro.TMP_InputField:LateUpdate()
    9. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    10.  
    11. text: , charIndex: 0, addedChar: 1
    12. BirthDateFormat:OnValidateInput(String, Int32, Char)
    13. TMPro.TMP_InputField:LateUpdate()
    14. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    15.  
    16. 1 is digit : True
    17. BirthDateFormat:OnValidateInput(String, Int32, Char)
    18. TMPro.TMP_InputField:LateUpdate()
    19. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    20.  
    21. text: 1, charIndex: 1, addedChar: 2
    22. BirthDateFormat:OnValidateInput(String, Int32, Char)
    23. TMPro.TMP_InputField:LateUpdate()
    24. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    25.  
    26. / added
    27. BirthDateFormat:OnValidateInput(String, Int32, Char)
    28. TMPro.TMP_InputField:LateUpdate()
    29. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    30.  
    31. text: , charIndex: 0, addedChar: 1
    32. BirthDateFormat:OnValidateInput(String, Int32, Char)
    33. TMPro.TMP_InputField:LateUpdate()
    34. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    35.  
    36. 1 is digit : True
    37. BirthDateFormat:OnValidateInput(String, Int32, Char)
    38. TMPro.TMP_InputField:LateUpdate()
    39. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    40.  
    41. text: 1, charIndex: 1, addedChar: 2
    42. BirthDateFormat:OnValidateInput(String, Int32, Char)
    43. TMPro.TMP_InputField:LateUpdate()
    44. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    45.  
    46. / added
    47. BirthDateFormat:OnValidateInput(String, Int32, Char)
    48. TMPro.TMP_InputField:LateUpdate()
    49. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    50.  
    51. text: 12/, charIndex: 3, addedChar: /
    52. BirthDateFormat:OnValidateInput(String, Int32, Char)
    53. TMPro.TMP_InputField:LateUpdate()
    54. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    55.  
    56. / is digit : False
    57. BirthDateFormat:OnValidateInput(String, Int32, Char)
    58. TMPro.TMP_InputField:LateUpdate()
    59. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    60.  
    61. text: 12/, charIndex: 3, addedChar: 3
    62. BirthDateFormat:OnValidateInput(String, Int32, Char)
    63. TMPro.TMP_InputField:LateUpdate()
    64. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    65.  
    66. 3 is digit : True
    67. BirthDateFormat:OnValidateInput(String, Int32, Char)
    68. TMPro.TMP_InputField:LateUpdate()
    69. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    70.  
    71. text: , charIndex: 0, addedChar: 1
    72. BirthDateFormat:OnValidateInput(String, Int32, Char)
    73. TMPro.TMP_InputField:LateUpdate()
    74. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    75.  
    76. 1 is digit : True
    77. BirthDateFormat:OnValidateInput(String, Int32, Char)
    78. TMPro.TMP_InputField:LateUpdate()
    79. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    80.  
    81. text: 1, charIndex: 1, addedChar: 2
    82. BirthDateFormat:OnValidateInput(String, Int32, Char)
    83. TMPro.TMP_InputField:LateUpdate()
    84. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    85.  
    86. / added
    87. BirthDateFormat:OnValidateInput(String, Int32, Char)
    88. TMPro.TMP_InputField:LateUpdate()
    89. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    90.  
    91. text: 12/, charIndex: 3, addedChar: /
    92. BirthDateFormat:OnValidateInput(String, Int32, Char)
    93. TMPro.TMP_InputField:LateUpdate()
    94. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    95.  
    96. / is digit : False
    97. BirthDateFormat:OnValidateInput(String, Int32, Char)
    98. TMPro.TMP_InputField:LateUpdate()
    99. (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    For whatever reason, on iOS it's stuck on "/". And I was doing 1, 2, 3, but logs show several time 1... very weird!
     
  2. kdarius43

    kdarius43

    Joined:
    Mar 16, 2015
    Posts:
    140
    Did you ever find a solution to this?
     
  3. -Aymeric-

    -Aymeric-

    Joined:
    Oct 21, 2014
    Posts:
    95
    @kdarius43 Unfortunately no, I end up with a dirty script:
    Code (CSharp):
    1. using TMPro;
    2. using UnityEngine;
    3.  
    4. public class BirthDateFormat : MonoBehaviour {
    5.  
    6.     public TMP_InputField input;
    7.  
    8.     bool allSlashAdded;
    9.  
    10.     public void OnValueChanged() {
    11.  
    12.         if (!allSlashAdded && (input.text.Length == 2 || input.text.Length == 5))
    13.         {
    14.             allSlashAdded = input.text.Length == 5;
    15.  
    16.             input.text += "/";
    17.  
    18.             input.stringPosition = input.text.Length;
    19.         }
    20.     }
    21. }