Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to validate a text box if it contains only numeric value which can be used for calculation
i'm using (String.IsNullOrEmpty(textBox1.Text)
but it is still giving error if any char or sting is entered in that textbox.
Posted

Hi,

Use regular expression to make sure that whatever text entered is numeric i.e a combination of 0-9 character.

http://www.regular-expressions.info/

http://regexlib.com/
 
Share this answer
 
simple...use regular expression validator.
 
Share this answer
 
Hello,

Kindly find the regular expression for allowing numbers in textbox below:
Regex.IsMatch(txtbox.Text, @"^\d+$");
Kindly, let me know if you need further help!

Thanks,
Paras Sanghani
Mark As Answer if it helped you.
 
Share this answer
 
Now TextBox in Windows application of C# have something called as events
all u have to do is use the KeyPress event of the textbox and paste in the following code...

The below code will work with integers a better option would be to use regular expression

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //only numbers code

            if (char.IsNumber(e.KeyChar) == false)
            {
                e.Handled = true;
            }
            //backspace workin keycode
            if (e.KeyChar == (char)Keys.Back)
            {
                e.Handled = false;
            }
            //if u want that alphabets must be entered then just use
           // char.IsLower in place of char.IsNumber
        }


and ur done...

Do rate my answer once u find it useful

Thanks & Regards
Radix :rose:
 
Share this answer
 

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