Date input field, adding char issue

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!

Did you ever find a solution to this?

@kdarius43_1 Unfortunately no, I end up with a dirty script:

using TMPro;
using UnityEngine;

public class BirthDateFormat : MonoBehaviour {

    public TMP_InputField input;

    bool allSlashAdded;

    public void OnValueChanged() {

        if (!allSlashAdded && (input.text.Length == 2 || input.text.Length == 5))
        {
            allSlashAdded = input.text.Length == 5;

            input.text += "/";

            input.stringPosition = input.text.Length;
        }
    }
}

Above format script doesn’t work with Delete key.
Here is updated script.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class BirthDateFormat : MonoBehaviour {

    public InputField input;

    int prevLength;

    void Start () {
        input.onValueChanged.AddListener (OnValueChanged);
    }

    public void OnValueChanged (string str) {
        print ("String:" + str);
        if (str.Length > 0) {
            input.onValueChanged.RemoveAllListeners ();
            if (!char.IsDigit (str[str.Length - 1]) && str[str.Length - 1] != '/') { // Remove Letters
                input.text = str.Remove (str.Length - 1);
                input.caretPosition = input.text.Length;
            } else if (str.Length == 2 || str.Length == 5) {
                if (str.Length < prevLength) { // Delete
                    input.text = str.Remove (str.Length - 1);
                    input.caretPosition = input.text.Length;
                } else { // Add
                    input.text = str + "/";
                    input.caretPosition = input.text.Length;
                }
            }
            input.onValueChanged.AddListener (OnValueChanged);
        }
        prevLength = input.text.Length;
    }
}
3 Likes

can I ask where i put this code and how connect?