Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I solve this problem
C#
private void txtTestingNumeric_TextChanged(object sender, EventArgs e)
       {
           if (System.Text.RegularExpressions.Regex.IsMatch("[^0-9]", txtTestingNumeric.Text))
           {
               MessageBox.Show("Please enter only numbers.");
               txtTestingNumeric.Text.Remove(txtTestingNumeric.Text.Length - 1);
           }
       }

Please help me
thanks in advance
Posted
Updated 29-Jul-13 22:08pm
v2
Comments
Hem Raj Thakur 30-Jul-13 3:44am    
This will create a same error if you press back space on this textbox multitime
Ankur\m/ 30-Jul-13 4:08am    
Edited to add c# tag. Please tag your question properly to get accurate answers.

use the keyPress event of textbox like this
C#
private void txtTestingNumeric_KeyPress(object sender, KeyPressEventArgs e)
       {
           int ascii = e.KeyChar;
           if ((ascii >= 48 && ascii <= 57) || ascii == 8 || ascii == 13 || ascii==46)
           {
               e.Handled = false;
           }
           else
           {
               e.Handled = true;
           }
       }
 
Share this answer
 
Comments
Thomas Daniels 30-Jul-13 4:00am    
+5!
Thanks7872 30-Jul-13 4:03am    
Why to do this in code while we can check it in definition itself? See my solution.
Thomas Daniels 30-Jul-13 4:04am    
Who says that the OP uses ASP.NET? My guess was Windows Forms because MessageBox.Show was used.
Thanks7872 30-Jul-13 4:42am    
Your guess was Windows Forms,but my guess is ASP.NET. The question is not tagged properly,so this point doesnt stands. I see no reason for down voting. He is just intended to use numeric textbox and i provided that.
Thomas Daniels 30-Jul-13 4:46am    
Rohan Leuva wrote:
I see no reason for down voting

I didn't downvote you.
C#
private void TectBox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if ((e.KeyChar >= 48 && e.KeyChar <= 57) )
           {
               e.Handled = false;
           }
           else
           {

               e.Handled = true;
           }
       }
 
Share this answer
 
Comments
Thomas Daniels 30-Jul-13 4:02am    
Reason for my vote of 3: your code is almost correct, but you should also allow a backspace (KeyChar for a backspace is 8)
This Is Best Code.. It will allow you to use only numbers and Space and Backspace
C#
private void txttax1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if ((e.KeyChar >= 48 && e.KeyChar <= 57) || (e.KeyChar == 32) || (e.KeyChar == 8))
           {
               e.Handled = false;
           }
           else
           {

               e.Handled = true;
           }
       }
 
Share this answer
 
v2
C#
private void txtTestingNumeric_KeyPress(object sender, EventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
    {
        e.Handled = true;
    }
}
 
Share this answer
 
C#
private void txtamount_KeyDown(object sender, KeyEventArgs e)
      {
          try
          {
              if (e.KeyValue >= 48 && e.KeyValue <= 57 || e.KeyValue == 17|| e.KeyValue == 20 || e.KeyValue == 92 || e.KeyValue == 46 || e.KeyValue == 45 || e.KeyValue == 18 || e.KeyValue == 91 || e.KeyValue == 32 || e.KeyValue == 93 || e.KeyValue == 16 || e.KeyValue == 13 || e.KeyValue == 8 || e.KeyValue == 33 || e.KeyValue == 34 || e.KeyValue == 35 || e.KeyValue == 36 || e.KeyValue == 37 || e.KeyValue == 38 || e.KeyValue == 39 || e.KeyValue == 40 || e.KeyValue == 27 || e.KeyValue == 144 || e.KeyValue == 96 || e.KeyValue == 97 || e.KeyValue == 98 || e.KeyValue == 99 || e.KeyValue == 100 || e.KeyValue == 101 || e.KeyValue == 102 || e.KeyValue == 103 || e.KeyValue == 104 || e.KeyValue == 105 || e.KeyValue == 106 || e.KeyValue == 107 || e.KeyValue == 108 || e.KeyValue == 109 || e.KeyValue == 110 || e.KeyValue == 111 || e.KeyValue == 112 || e.KeyValue == 113 || e.KeyValue == 114 || e.KeyValue == 115 || e.KeyValue == 116 || e.KeyValue == 117 || e.KeyValue == 118 || e.KeyValue == 119 || e.KeyValue == 120 || e.KeyValue == 121 || e.KeyValue == 22 || e.KeyValue == 123 || e.KeyValue == 145 || e.KeyValue == 19)
              {

              }
              else
              {
                  if (e.Alt && e.KeyCode == Keys.N || e.Alt && e.KeyCode == Keys.S || e.Alt && e.KeyCode == Keys.E || e.Alt && e.KeyCode == Keys.C)
                  {

                  }
                  else
                  {
                      MessageBox.Show("Ooops! Only numeric allowed.", "School Charge Master", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                      try
                      {
                          txtamount.Text = "";
                          txtamount.Focus();
                      }
                      catch (Exception)
                      { }
                  }
              }
          }
          catch
          {
          }
      }

this is for (.) allow e.KeyValue == 190
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900