I have an inputfield in one scene,there I need a formate like MM/YY,when user enters first two integers /
should be added immediate to the second character.So I have added following code the inputfield onvalueChanged event
if (ExpiryDateInputField.text.Length == 2)
{
ExpiryDateInputField.text = ExpiryDateInputField.text + "/";
ExpiryDateInputField.MoveTextEnd(false);
}
Here the problem is,when I am trying to remove text the text after /
is removing but the text before /
is not removing,Where I am doing wrong.
Ex: If I enter 1234 the input field will display is as 12/34,when I am trying to remove the entered text 3 and 4 are removing while clicking on backspace on keyboard but 12/
are not removing. Is there any way to work it out?
The problem is basically this.
You enter "12", OnValueChange is called and "/" is added, now you have "12/".
You press backspace to remove the "/", OnValueChange is called and "/" is added, now you have "12/".
You could try remembering the last value of the InputField
, and if that value contained a "/", dont append another "/".
string last = string.Empty;
public void OnValueChange()
{
if (ExpiryDateInputField.text.Length == 2 && !last.Contains("/"))
{
ExpiryDateInputField.text = ExpiryDateInputField.text + "/";
ExpiryDateInputField.MoveTextEnd(false);
}
last = ExpiryDateInputField.text;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.