Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I've a textbox in my forum and I want user to enter only int values

C#



if(textbox1.text==int)
{
\\Calculations
}

else
{
\\Error
}
Posted
Comments
PrashantSonewane 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.
Zain ul abdeen 12-Apr-13 1:20am    
its a winfrom. How to check the key code?
Sergey Alexandrovich Kryukov 12-Apr-13 1:49am    
How WinForm can be in your "forum"?!
—SA
Prasad Khandekar 12-Apr-13 1:40am    
You can use MaskedTextBox control.

Hello,

Here is a example directly taken from MSDN. You will have to modify it a bit to suite your needs,
C#
// 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,
 
Share this answer
 
C#
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyCode != Keys.Back )
{
e.Handled = true;
}
}
 
Share this answer
 
also try this code..it will accept only int value
C#
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), @"^[0-9\b?]$"))
                e.Handled = true;
        }
 
Share this answer
 
v2
Comments
Zain ul abdeen 12-Apr-13 11:24am    
Its Working but how to include backspace also
Pallavi Waikar 12-Apr-13 12:45pm    
check updated answer
Zain ul abdeen 13-Apr-13 0:14am    
Thankyou 1 more thing can you help me out I want to have autocomplete for textbox from database
You can validate that in javascript. Use the following code.

JavaScript
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.NET
<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.
 
Share this answer
 
Dear C# has predefined function to check if the entered character is digit or alphabet on keypress event.

C#
//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.
 
Share this answer
 
v2
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
 
Share this answer
 
v2
Comments
Zain ul abdeen 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)



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