UWP textbox 只能输入数字

时间:2022-07-24 00:05:08

private void Testbox_TextChanged(object sender, TextChangedEventArgs e)
{
    var textbox = (TextBox)sender;
    if (!Regex.IsMatch(textbox.Text, "^\\d*\\.?\\d*$") && textbox.Text != "")
   {
     int pos = textbox.SelectionStart - 1;
     textbox.Text = textbox.Text.Remove(pos, 1);
     textbox.SelectionStart = pos;
   }

}

只能输入字符和数组的正则表达式:"^[A-Za-z0-9]+$"