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):
using TMPro;
using UnityEngine;
public class BirthDateFormat : MonoBehaviour {
public TMP_InputField input;
bool allSlashAdded;
void Start()
{
input.onValidateInput += OnValidateInput;
}
char OnValidateInput(string text, int charIndex, char addedChar)
{
Debug.Log("text: " + text + ", charIndex: " + charIndex + ", addedChar: " + addedChar);
if (!allSlashAdded && (input.text.Length == 1 || input.text.Length == 4))
{
allSlashAdded = input.text.Length == 5;
input.text = text + addedChar + "/";
input.stringPosition = input.text.Length;
Debug.Log("/ added");
return '\0';
}
Debug.Log(addedChar + " is digit : " + char.IsDigit(addedChar));
return char.IsDigit(addedChar) ? addedChar : '\0';
}
void OnDestroy()
{
input.onValidateInput -= OnValidateInput;
}
}
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)!
text: , charIndex: 0, addedChar: 1
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
1 is digit : True
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
text: , charIndex: 0, addedChar: 1
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
1 is digit : True
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
text: 1, charIndex: 1, addedChar: 2
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
/ added
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
text: , charIndex: 0, addedChar: 1
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
1 is digit : True
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
text: 1, charIndex: 1, addedChar: 2
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
/ added
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
text: 12/, charIndex: 3, addedChar: /
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
/ is digit : False
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
text: 12/, charIndex: 3, addedChar: 3
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
3 is digit : True
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
text: , charIndex: 0, addedChar: 1
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
1 is digit : True
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
text: 1, charIndex: 1, addedChar: 2
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
/ added
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
text: 12/, charIndex: 3, addedChar: /
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
/ is digit : False
BirthDateFormat:OnValidateInput(String, Int32, Char)
TMPro.TMP_InputField:LateUpdate()
(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!