用正则表达式判断文本框输入内容是否正确

本文介绍了C#中的两个方法:GetNum用于使用正则表达式验证输入是否为数字,Input_Check用于在用户输入非数字时自动清理并高亮显示。方法针对TextBox控件进行操作,确保输入合规。
摘要由CSDN通过智能技术生成

   public bool GetNum(string str)
        {
            return Regex.IsMatch(str, @"^\d+[.]?\d*$");
        }
        public void Input_Check(string name)
        {
            object o = Form1.form.GetType().GetField(name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(检查表.Form1.form);
            if (((TextBox)o).Text == "")
            {
                ((TextBox)o).Text = "";
                ((TextBox)o).Select(((TextBox)o).Text.Length, 0);
            }
            else if (GetNum(((TextBox)o).Text) == false)
            {
                ((TextBox)o).Text = ((TextBox)o).Text.Substring(0, ((TextBox)o).Text.Length - 1);
                ((TextBox)o).Select(((TextBox)o).Text.Length, 0);
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值