winform判断输入是否是数字

 1  private bool IsNum(string str)
 2         {
 3             try
 4             {
 5                 foreach (char c in str)
 6                 {
 7                     if (char.IsDigit(c))
 8                         return true;
 9                     return false;
10                 }
11             }
12             catch (Exception ex)
13             {
14                 MessageBox.Show(ex.Message);
15             } 
16             return false;
17         }

正则表达式:

1 Regex r=new Regex(@^\d+(\.)?\d*$);
2 if(r.IsMatch(this.TextBox1.Text))
3 {
4 this.Response.Write(是数字);
5 }
6 else
7 {
8 this.Response.Write(不是数字);
9 }

 

 

 

输入的是不是字母:

1 foreach (char c in str)
2                 {
3                     if (char.IsLetter(c))
4                         return true;
5 
6                     return false;
7                 }

 

posted on 2015-03-18 09:22  ultrastrong  阅读(820)  评论(0编辑  收藏  举报