Click here to Skip to main content
15,915,089 members
Please Sign up or sign in to vote.
3.60/5 (3 votes)
See more:
i want to validate my textbox that only contains numerics values nothing else in C# windows form application
Posted
Updated 24-Jan-17 23:57pm
v2
Comments
BillWoodruff 7-Feb-14 21:59pm    
In a question like this, the details are important: do you wish to allow the use of the minus-sign, of the decimal point ?

Try like this
C#
public Form1()
{
    InitializeComponent();
    txtHomePhone.KeyPress += new KeyPressEventHandler(txtHomePhone_KeyPress);
}
private void txtHomePhone_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '') //The  character represents a backspace
    {
        e.Handled = false; //Do not reject the input
    }
    else
    {
        e.Handled = true; //Reject the input
    }
}


Refer:
validating-whether-a-textbox-contains-only-numbers[^]
validating-a-textbox-to-allow-only-numeric-values[^]
 
Share this answer
 
Please try is as below.

C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar)
        && !char.IsDigit(e.KeyChar)
        && e.KeyChar != '.')
    {
        e.Handled = true;
    }

    // only allow one decimal point
    if (e.KeyChar == '.'
        && (sender as TextBox).Text.IndexOf('.') > -1)
    {
        e.Handled = true;
    }
}



For more info : How do I make a textbox that only accepts numbers
 
Share this answer
 
You can write code in two ways:

1. In the Textbox's KeyPress event, tap the KeyChar and if it is not not within the range for numebers, set the KeyChar to 0.

2. In the Textbox's Validate event, Check the contents with int.TryParse or double.TryParse as the case may be and alert the user.

Doing both of the above will ensure that your textbox contains only numbers.
 
Share this answer
 
v2
C#
private void amounttext_KeyDown(object sender, KeyEventArgs e)
 {
     try
     {
         string name = this.Text.ToString();
         if ((e.KeyValue >= 48 && e.KeyValue <= 57)|| e.KeyValue == 17 || 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 == 192 || 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 == 122 || e.KeyValue == 123 || e.KeyValue == 145 || e.KeyValue == 19|| e.KeyValue == 20)
         {

         }
         else
         {
             MessageBox.Show("Only numeric value is allowed !", "" + name.ToString() + "", MessageBoxButtons.OK, MessageBoxIcon.Warning);


             string varlenth = "";
             varlenth = amounttext.Text.Substring(0, amounttext.Text.Length - 1);
             amounttext.Text = varlenth;
             amounttext.Select(amounttext.Text.Length, 0);
         }
     }
     catch { }
 }
 
Share this answer
 
v3
Comments
CHill60 7-Feb-14 8:16am    
You're probably going to get a lot of downvotes for this so it's only fair that I let you know *some* of the things wrong with this...
1. This is fundamental. You have mixed AND and OR together without using brackets... what would you expect the result of if (e.KeyValue >= 48 && e.KeyValue <= 57 || e.KeyValue == 17) to be? Better would be if ( (e.KeyValue >= 48 && e.KeyValue <= 57) || e.KeyValue == 17)
2. KeyValue 97 through 122 are lowercase LETTERS ... i.e. not numeric. There are several other non-numeric characters in there too. See solution 2 - .NET provides functions for determining if a character is a digit
3. You have typing errors ... e.KeyValue ==22 instead of e.KeyValue = 122 - always TEST your solutions (or tell the OP that you haven't)
Hem Raj Thakur 7-Feb-14 8:33am    
thank you sir for improve my mistake. but other code is run successfully.
sorry if it matches with the other answers but this is quite different
public static void IsNumberic(KeyPressEventArgs e, System.Windows.Forms.TextBox Tb)
{
    if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
    {
        e.Handled = false;
    }
    else
    {
        e.Handled = true;
    }
}
 
Share this answer
 
Well i would like to tell you see this article here on CP:
Check If A String Value Is Numeric[^]
And for using it on button click usage you may see this one:
Validate Textbox is Numeric on ButtonClick in C#[^]
 
Share this answer
 
private void TextboxName_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back)
e.Handled = true;
}

For digits and backspace only in c#
 
Share this answer
 
v2
Comments
Richard MacCutchan 25-Jan-17 6:13am    
Please do not resurrect old questions. This one is three years old.

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