【发布时间】:2019-09-08 00:26:21
【问题描述】:
我想将文本框设为数字,我尝试了这个功能:
<TextBox Name="txtProductId" PreviewTextInput="Number_Validation"/>
public static void Number_Validation(object sender,System.Windows.Input.TextCompositionEventArgs e)
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
但它接受“空格”和数字。我不需要空间。
【问题讨论】:
-
^[0-9]*$,试试看.. 或[\d] -
当我使用
^[0-9]*$它接受任何字母和数字时,[\d]对我不起作用 -
您可以从其他 SO 答案中剪切和粘贴样本。例如stackoverflow.com/questions/1268552/…
-
看看this post,它让您有机会仅在文本框中接受数值。