Hello

I have one textBox...let say i want some to enter their name in textbox but i dont want user to enter integer value in text ONLY character.

can any one here tell how to make program for this OR any inbuilt function is there.

I m using visual studio 2005

Thanks

well i m beginning in c#....DdoubleD solution is over my head
i m using following code...but it is not working propeerly

string s;
            int i,c;
            c=textBox1.Text.Length;
            for(i=0; i<c-1; i++)
            {
                if(textBox1.Text=="1" || textBox1.Text=="2")
                {
                    textBox1.Text = "";
                    MessageBox.Show("Only Characters Allowed");
                }
            }

        }

This code will limit allowed character input to a-z and A-Z characters:

const string regExpr = "[a-z]";
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(e.KeyChar.ToString().ToLower(), regExpr);
            if (!m.Success)
                e.Handled = true;
        }

You need to add the event handler in your form's constructor after the InitializeControls() call:

this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);

or, you can use the designer to add the event handler to the KeyPress event.

I should follow the advise of DdoubleD and at least try to understand the code he proposes. If some part of it is "above your head" let us know.
Line 4 of your code should be : for(i = 0; i < textBox1.Text.Length; i++)

well you are not mentioned that it is your win application, or ASP application.
If you are using ASP, than there you have inbuilt validation controls, you can use that.

You can also take a look at char.IsNumber() :

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (!char.IsNumber(e.KeyChar))
        e.Handled = true;
    }

There is still a few more problems though -- You can right click on the text box and select "paste" and that will allow pasting letters in to the control. I always perform validation one last time before a saving mechanism fires since there are a number of ways you can get around control input validation.

private void Save()
    {
      if (!ValidNumber(textBox1.Text))
      {
        textBox1.Focus();
        MessageBox.Show("Invalid input...");
        textBox1.Focus();
      }
    }

    private static bool ValidNumber(string s)
    {
      int i;
      return int.TryParse(s, out i);
    }
commented: always worth mentioning...keypress validation is far from perfect +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.