winformは入力が数字かどうかを判断する

4338 ワード

 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                 }