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

  1. 方法一:  
  2.   
  3. private void tBox_KeyPress(object sender, KeyPressEventArgs e)  
  4.   
  5.  {  
  6.             if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键  
  7.             if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数  
  8.             if (e.KeyChar > 0x20)  
  9.             {  
  10.                 try  
  11.                 {  
  12.                     double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());  
  13.                 }  
  14.                 catch  
  15.                 {  
  16.                     e.KeyChar = (char)0;   //处理非法字符  
  17.                 }  
  18.             }  
  19. }  
  20.   
  21. 方法二:  
  22.   
  23. private void TextBox_KeyPress(object sender, KeyPressEventArgs e)  
  24.  {  
  25.     if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))  
  26.     {  
  27.       e.Handled = true;  
  28.     }  
  29. }  
  30. 或者  
  31.   
  32. private void TextBox_KeyPress(object sender, KeyPressEventArgs e)  
  33. {  
  34.     if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))  
  35.     {  
  36.       e.Handled = true;  
  37.     }  
  38.   
  39. }  
  40.   
  41. 方法三:  
  42.   
  43. private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)  
  44. {  
  45. if(e.KeyChar!='\b')//这是允许输入退格键  
  46. {  
  47. if((e.KeyChar<'0')||(e.KeyChar>'9'))//这是允许输入0-9数字  
  48. {  
  49. e.Handled = true;  
  50. }  
  51. }  
  52. }  
  53.   
  54. 方法四:  
  55.   
  56. private void textBox1_Validating(object sender, CancelEventArgs e)   
  57. {   
  58. const string pattern = @"^\d+\.?\d+{1}quot;;   
  59. string content = ((TextBox)sender).Text;   
  60.   
  61. if (!(Regex.IsMatch(content, pattern)))   
  62. {   
  63. errorProvider1.SetError((Control)sender, "只能输入数字!");   
  64. e.Cancel = true;   
  65. }   
  66. else   
  67. errorProvider1.SetError((Control)sender, null);   
  68. }  
  69.   
  70. 方法五:  
  71.   
  72. private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)  
  73. {  
  74. if(e.KeyChar=='.' && this.textBox1.Text.IndexOf(".")!=-1)  
  75. {  
  76. e.Handled=true;  
  77. }  
  78.   
  79. if(!((e.KeyChar>=48 && e.KeyChar<=57) || e.KeyChar=='.' || e.KeyChar==8))  
  80. {  
  81. e.Handled=true;  
  82. }  
  83.   
  84. }  
  85.   
  86. 方法六:  
  87.   
  88. private void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e)  
  89. {  
  90.             if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))  
  91.             {  
  92.                 e.Handled = true;//消除不合适字符  
  93.             }  
  94.             else if (Char.IsPunctuation(e.KeyChar))  
  95.             {  
  96.                 if (e.KeyChar != '.' || this.textBox1.Text.Length == 0)//小数点  
  97.                 {  
  98.                     e.Handled = true;  
  99.                 }  
  100.                 if (textBox1.Text.LastIndexOf('.') != -1)  
  101.                 {  
  102.                     e.Handled = true;  
  103.                 }  
  104.             }        
  105.   }    
  106.   
  107. 方法七:  
  108.   
  109. 利用ASCII码处理办法、  
  110. {  
  111.   
  112.             if ((e.KeyChar <= 48 || e.KeyChar >=57) && (e.KeyChar != 8) && (e.KeyChar != 46))  
  113.               e.Handled = true;  
  114. ================48代表0,57代表9,8代表空格,46代表小数点  
  115. }

猜你喜欢

转载自blog.csdn.net/JessicaQNL/article/details/82979491