I am making a player registration menu in a game using data such as: Name, Email, Password, Gender, Date of Birth and Phone Number.
The problem is when I add the UI InputField for the Date of Birth and Phone Number, I would need to apply a formatting mask such as “##/ ##/####” for the Date and “(##) #####-####” for the Phone.
I can limit the InputField to typing just numbers, however I did not find any way to add formatted masks or to create a component akin to C#'s MaskedTextBox. Is it possible to do something similar to this in Unity?
public void OnValueChanged(string input) // Supposing input = "953"
{
int index = 0 ;
string format = "##/##/####" ;
string output = format;
for( int i = 0 ; i < input.Length ; ++i )
{
index = output.IndexOf("#");
if( index < 0 )
break ;
if( index == 0 )
output = input *+ output.Substring ( 1 );*
else if( index == output.Length - 1 ) output = output.Substring ( 0, index ) + input ; else output = output.Substring ( 0, index ) + input + output.Substring ( index + 1 ); } if( index >= 0 ) output = output.Substring( 0, index + 1 );
Debug.Log( output ); // Will output 95/3 } ----- ###2nd UPDATE public UnityEngine.UI.InputField inputField; // The Text component showing your formatted string public UnityEngine.UI.Text text; private string lastValidInput = string.Empty; private void Awake() { inputField.onValueChanged.AddListener( OnValueChanged ); } private void OnValueChanged( string input ) // Supposing input = “9534” { string format = “{0}{1}/{2}{3}/{4}{5}{6}{7}” ; string[] array = new string[input.Length]; int index = format.IndexOf(“{” + (input.Length - 1) + “}”); if ( input.Length == 0 ) { text.text = string.Empty; return; } if ( index >= 0 ) { format = format.Substring( 0, index + 3 ); } else { inputField.text = lastValidInput; return; } for ( index = 0 ; index < input.Length ; ++index ) array[index] = string.Empty + input[index]; lastValidInput = input; text.text = string.Format( format, array ); // Will output 95/34 } ###2nd Update’ (different output) private void OnValueChanged( string input ) // Supposing input = “9534” { string format = “{0}{1}/{2}{3}/{4}{5}{6}{7}” ; string[] array = new string[input.Length]; int index = format.IndexOf(“{” + (input.Length) + “}”); if ( input.Length == 0 ) { text.text = string.Empty; return; } if ( index >= 0 ) { format = format.Substring( 0, index ); } else { inputField.text = lastValidInput; return; } for ( index = 0 ; index < input.Length ; ++index ) array[index] = string.Empty + input[index]; lastValidInput = input; text.text = string.Format( format, array ); // Will output 95/34/ } #IMPORTANT NOTE : The functions I gave you will only accept a string containing numbers only. Thus, you will need an input field with invisible characters to accept the input and a Text component to show the formatted result (See 2nd Update) ----- ##INITIAL ANSWER I think [regular expressions][1] is the way to go. I prepared you an example for your first case : System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(“9545551212”, @“(\d{3})(\d{3})(\d{4})”); if ( match.Success ) { // match.Groups[0] will return the whole input string output = string.Format(“({0}) {1}-{2}”, match.Groups[1], match.Groups[2], match.Groups[3]); Debug.Log( output ); // Will log “(954) 555-1212” } _*[1]: Microsoft Learn: Build skills that open doors in your career