UWP textbox 只能输入数字

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]+$"

时间: 2024-07-30 05:40:32

UWP textbox 只能输入数字的相关文章

C#设置textBox只能输入数字(正数,负数,小数)简单实现

/* *设置textBox只能输入数字(正数,负数,小数) */ public static bool NumberDotTextbox_KeyPress(object sender, KeyPressEventArgs e) { //允许输入数字.小数点.删除键和负号 if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e

Asp.net TextBox只能输入数字

原文:Asp.net TextBox只能输入数字 <asp:textbox id="TextBox1" onkeyup="if(isNaN(value))execCommand('undo')" runat="server" Width="80px" onafterpaste="if(isNaN(value))execCommand('undo')"></asp:textbox> 其

wpf 限制textbox只能输入数字及特殊键

原文:wpf 限制textbox只能输入数字及特殊键 使用vs2015进行wpf设计时,发现限制输入数字时,Keydown事件无法检测到空格的输入,后经过查资料调试,发现PreviewKeyDown可以满足检测输入所有值合法性的要求,我的textbox输入框中只允许输入(大小键盘中)的数字0-9,以及删除回车,左右方向键,tab键,删除键,下面代码为限制 private void MinSpaceTextBox_PreviewKeyDown(object sender, KeyEventArgs

winform 中TextBox只能输入数字

textBox1.KeyPress+=TextNumber_KeyPress; private void TextNumber_KeyPress(object sender, KeyPressEventArgs e) { var tb = (TextBox) sender; if (e.KeyChar != Convert.ToChar(13)) { if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar)) { MessageShow("只能

Asp.net控制TextBox只能输入数字

在Asp.net开发中,为了确保数据的正确性,经常要对用户输入的内容进行验证,比如说用户只能输入数字,不能输入中文和英文,这如何实现呢?下面我们来看看.    实现过程,在TextBox控件中加两个属性事件:<asp:TextBox ID="txtName" runat="server" Width="175px" style="ime-mode:disabled" onkeypress="if (event.

C# TextBox 只能输入数字

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { TextBox txt = sender as TextBox; //屏蔽非法按键,只能输入小数 if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '-' || e.KeyChar == '.' || e.KeyChar == 8) { if (txt.Text.Contai

textbox只能输入数字或中文的常用正则表达式和验证方法

验证数字的正则表达式集 验证数字:^[0-9]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(0|[1-9][0-9]*)$ 验证有两位小数的正实数:^[0-9]+(.[0-9]{2})?$ 验证有1-3位小数的正实数:^[0-9]+(.[0-9]{1,3})?$ 验证非零的正整数:^\+?[1-9][0-9]*$ 验证非零的负整数:^\-[1-9][0-9]*$ 验证非负整数(正整数 + 0):^\d

WPF限制TextBox只能输入数字

KeyDown事件private void tbCount_KeyDown(object sender, KeyEventArgs e) { TextBox txt = sender as TextBox; //屏蔽非法按键 if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key.ToString() == "Tab") { if (txt.Tex

[.NET] 在WPF中实现TextBox只能输入数字

如题,实现此功能的方法有很多,如果使用的是MVVM的开发模式,该如何实现此功能? 很简单,为TextBox新建一个附加属性,就可以完美的解决此问题. 新建一个附件属性 IsOnlyAcceptFigure ,并为其添加属性回调函数即可. 代码如下: namespace WPF.Validation { public class TextBoxOnlyAcceptFigureBehavior { public static readonly DependencyProperty IsOnlyAcc