Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# string TextBoxBase
I've a textbox in my forum and I want user to enter only int values
 

 
if(textbox1.text==int)
{
\\Calculations
}
 
else
{
\\Error
}
Posted 11-Apr-13 19:57pm
Comments
PrashantSonewane at 12-Apr-13 1:05am
   
Is textbox on ASP .NET page or winform? If on ASP page then use javascript function (with regular expression or reading keycode). In Winform you can check the keycode on textchange event.
Zainulabdeen at 12-Apr-13 1:20am
   
its a winfrom. How to check the key code?
Sergey Alexandrovich Kryukov at 12-Apr-13 1:49am
   
How WinForm can be in your "forum"?!
—SA
Prasad Khandekar at 12-Apr-13 1:40am
   
You can use MaskedTextBox control.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
Here is a example directly taken from MSDN. You will have to modify it a bit to suite your needs,
// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;
 
// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Initialize the flag to false.
    nonNumberEntered = false;
 
    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    {
        // Determine whether the keystroke is a number from the keypad.
        if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
        {
            // Determine whether the keystroke is a backspace.
            if(e.KeyCode != Keys.Back)
            {
                // A non-numerical keystroke was pressed.
                // Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;
            }
        }
    }
    //If shift key was pressed, it's not a number.
    if (Control.ModifierKeys == Keys.Shift) {
        nonNumberEntered = true;
    }
}
 
// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
    {
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;
    }
}
 
Regards,
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyCode != Keys.Back )
{
e.Handled = true;
}
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

also try this code..it will accept only int value
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), @"^[0-9\b?]$"))
                e.Handled = true;
        }
  Permalink  
v2
Comments
Zainulabdeen at 12-Apr-13 11:24am
   
Its Working but how to include backspace also
Pallavi Waikar at 12-Apr-13 12:45pm
   
check updated answer
Zainulabdeen at 13-Apr-13 0:14am
   
Thankyou 1 more thing can you help me out I want to have autocomplete for textbox from database
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can validate that in javascript. Use the following code.
 
 
       function Validate() {
            var txtValue = document.getElementById("MainContent_txtTestBox").value;
            var checkFlag = isNaN(txtValue);
            
            if (!checkFlag) {
                return true;
            }
            else {
                alert("Number entered is not an integer");
                return false;
            }
        }
 
For the textbox, just put the onkeyup event and specify the above function as the handler as shown below.
<asp:textbox id="txtTestBox" runat="server" onkeyup="Validate()" xmlns:asp="#unknown" />
 
Using JavaScript instead of server code (code-behind) would reduce the load on the server and improve performance.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Dear C# has predefined function to check if the entered character is digit or alphabet on keypress event.
 
//consider following code to get your problem resolved

Private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar))
{
//your code here
}
if (char.IsLetter(e.KeyChar))
{
//your code here
}
}
 

I guess it helps you.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Hi,
 
Use range validation control for numeric values to check it client site.
This will check it client site and save round trip to server.
 
<asp:RangeValidator
ControlToValidate="tbox1"
MinimumValue="0"
MaximumValue="2147483647"
Type="int"
EnableClientScript="false"
Text="The data must be between 0 and 2147483647!"
runat="server" />
 
2147483647 is the max value which we can use in ASP.NET
  Permalink  
v2
Comments
Zainulabdeen at 17-Apr-13 11:45am
   
Its a desktop software

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

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 15 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100