sonny5
sonny5

Reputation: 83

C# Textbox validation should only accept integer values, but allows letters as well

if (textBox1.Text != "")  // this forces user to enter something
{
  // next line is supposed to allow only 0-9 to be entered but should block all...
  // ...characters and should block a backspace and a decimal point from being entered....
  // ...but it is also allowing characters to be typed in textBox1
  if(!IsNumberInRange(KeyCode,48,57) && KeyCode!=8 && KeyCode!=46)  // 46 is a "."
  {  
     e.Handled=true;
  }
  else 
  {
     e.Handled=false;
  }  

  if (KeyCode == 13) // enter key
  {  
    TBI1 = System.Convert.ToInt32(var1);   // converts to an int
    Console.WriteLine("TBI1 (var1 INT)= {0}", var1);
    Console.WriteLine("TBI1= {0}", TBI1);
  } 

  if (KeyCode == 46)
  {
    MessageBox.Show("Only digits...no dots please!"); 
    e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar); 
  }
}
else
{
   Console.WriteLine("Cannot be empty!");
}

// If I remove the outer if statement and skip checking for an empty string, then
// it prevents letters from being entered in the textbox. I need to do both, prevent an 
// empty textbox AND prevent letters from being entered.
// thanks, Sonny5

Upvotes: 0

Views: 5955

Answers (4)

Akash
Akash

Reputation: 21

bool bV=false;
    private void textBox1_Validated(object sender, EventArgs e)
    {
        TextBox textBoxText = sender as TextBox;

        if (!textBoxText.Equals(String.Empty))  
        {
            foreach (char c in textBoxText.Text.ToArray())
            {
                if (!Char.IsDigit(c))
                {
                    if (!bV)
                    {
                        MessageBox.Show("Input value not valid plase Insert Integer Value");
                        bV = true;
                        textBox1.Text = String.Empty;
                        break;
                    }
                }
            }
        }
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        bV = false;
    }

Upvotes: 0

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102448

I think you could use the IsDigit function.

Something along these lines:

string textBoxText = "12kj3";

if (!textBoxText.Equals(String.Empty))  // this forces user to enter something
{
    foreach (char c in textBoxText.ToArray())
    {
        if (!Char.IsDigit(c))
        {
            //return false;
        }
    }

    //return true;
}
else
{
    Console.WriteLine("Cannot be empty!");
}

Hope you get the idea.

Upvotes: 1

Keith Rousseau
Keith Rousseau

Reputation: 4485

You can use the following RegEx to check that it is a number "^\d+$" and required.

Upvotes: 0

Franci Penov
Franci Penov

Reputation: 76021

You didn't specify where this code runs, but my assumption would be it runs on key down. Since key down is received before the character is processed and the Text property is updated, your check for .Text == "" will prevent the rest of the validation running, at least for the first character.

You should move the check for empty value on a different event than the check for the key pressed.

Upvotes: 2

Related Questions