C#判断多个textbox中不能为空且之能输入数字


 

foreach (Control c in groupBox1.Controls)
{
            if (c is TextBox)
        { 
            if (((TextBox)c).Text == null || ((TextBox)c).Text.Length == 0 || ((TextBox)c).Text == " ")//判断输入的数值不能为空
            {
                    MessageBox.Show("请输入数值!");
                     return;
              }

             if (!Validate(((TextBox)c).Text.Trim(), @"^(-?\d+)(\.\d+)?$"))//判断输入的数值只能是数值
            {
                     MessageBox.Show("只能输入数值!");
                     return;
             }
          }
}

static public bool Validate(string str, string regexStr)
{
     Regex regex = new Regex(regexStr);
     Match match = regex.Match(str);
     if (match.Success)
           return true;
     else
            return false;
}

注意:!!需要引用using System.Text.RegularExpressions;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM