Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Date input field, adding char issue

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

  1. -Aymeric-

    -Aymeric-

    Joined:
    Oct 21, 2014
    Posts:
    110
    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:
    170
    Did you ever find a solution to this?
     
  3. -Aymeric-

    -Aymeric-

    Joined:
    Oct 21, 2014
    Posts:
    110
    @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. }
     
  4. EmpireGameWorld

    EmpireGameWorld

    Joined:
    Jul 21, 2017
    Posts:
    3
    Above format script doesn't work with Delete key.
    Here is updated script.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7.  
    8. public class BirthDateFormat : MonoBehaviour {
    9.  
    10.     public InputField input;
    11.  
    12.     int prevLength;
    13.  
    14.     void Start () {
    15.         input.onValueChanged.AddListener (OnValueChanged);
    16.     }
    17.  
    18.     public void OnValueChanged (string str) {
    19.         print ("String:" + str);
    20.         if (str.Length > 0) {
    21.             input.onValueChanged.RemoveAllListeners ();
    22.             if (!char.IsDigit (str[str.Length - 1]) && str[str.Length - 1] != '/') { // Remove Letters
    23.                 input.text = str.Remove (str.Length - 1);
    24.                 input.caretPosition = input.text.Length;
    25.             } else if (str.Length == 2 || str.Length == 5) {
    26.                 if (str.Length < prevLength) { // Delete
    27.                     input.text = str.Remove (str.Length - 1);
    28.                     input.caretPosition = input.text.Length;
    29.                 } else { // Add
    30.                     input.text = str + "/";
    31.                     input.caretPosition = input.text.Length;
    32.                 }
    33.             }
    34.             input.onValueChanged.AddListener (OnValueChanged);
    35.         }
    36.         prevLength = input.text.Length;
    37.     }
    38. }
     
    VC2BDEV, itsnottme and DeniMN like this.
  5. shahad_s1l

    shahad_s1l

    Joined:
    Jan 25, 2021
    Posts:
    3
    can I ask where i put this code and how connect?