Unity InputField输入框时间校验
public static class InputFiledDateTimeFormat
{
/// <summary>
/// 输入框时间校验
/// </summary>
/// <param name="input"></param>
public static void DateTimeCkeck(this InputField input)
{
//监听输入字符
input.onValueChanged.AddListener(e =>
{
if (e.Length == 5)
{
if (System.Text.RegularExpressions.Regex.IsMatch(e.Substring(4, 1), @"([0-9]\d*\.?\d*)|(0\.\d*[1-9])"))
{
input.text = e.Substring(0, 4) + "-" + e.Substring(4);
input.MoveTextEnd(false);
}
else
{
input.text = e.Substring(0, 4);
input.MoveTextEnd(false);
}
}
else if (e.Length == 8)
{
if (System.Text.RegularExpressions.Regex.IsMatch(e.Substring(7, 1), @"([0-9]\d*\.?\d*)|(0\.\d*[1-9])"))
{
input.text = e.Substring(0, 7) + "-" + e.Substring(7);
input.MoveTextEnd(false);
}
else
{
input.text = e.Substring(0, 7);
input.MoveTextEnd(false);
}
}
else
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e, @"([0-9]\d*\.?\d*)|(0\.\d*[1-9])"))
{
input.text = "";
}
}
});
//编辑结束后校验
input.onEndEdit.AddListener(e =>
{
if (e.Length < 6)
{
MsgBox.instance.Show(true, "请输入完整时间", 1);
input.ActivateInputField();
return;
}
});
}
}