0

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

5
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by sknake
Featured Replies
  • 1
    ddanbe 2,525   7 Years Ago

    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++) Read More

  • 1
    sknake 1,622   7 Years Ago

    You can also take a look at [icode]char.IsNumber()[/icode]: [code] private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsNumber(e.KeyChar)) e.Handled = true; } [/code] 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 … Read More

0

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");
                }
            }

        }
0

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.

1

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++)

0

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.

1

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);
    }

Edited by sknake: n/a

Votes + Comments
always worth mentioning...keypress validation is far from perfect
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.