locked
Text box validation in c# windows form RRS feed

  • Question

  • Hi, I want to write the code for validation of text box which accept only characters, no numeric values, if user will type numeric values in text box it should show errror...........what code i will write for this................................?

    Actually i m working on my project Which is related to Hotel Management.

    • Moved by Vijaye Raji Friday, August 13, 2010 6:27 PM (From:Small Basic)
    Friday, August 13, 2010 6:18 AM

Answers

  • Hi GNIIT,

     

    Welcome to MSDN forums!

     I am interested in helping.

    I write a sample code, hope these help.

    private void textBox3_TextChanged(object sender, EventArgs e)

            {

                string context = this.textBox3.Text;

                bool isnum = false;

                for (int i = 0; i < context.Length;i++ )

                {

                    if (char.IsNumber(context[i]))

                    {

                        isnum = true;

                        break;

                    }

                }

                if (isnum)

                {

                    this.textBox3.Text = "error types";

                }

            }

     

    Hope these helps, if you have any problems, please feel free to let me know.

     

    Best Regards,

    Alan Chen

    ________________________________________

    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Alan_chen Friday, August 20, 2010 2:55 AM
    Tuesday, August 17, 2010 7:56 AM
  • You can do this with MaskedTextbox, which is an easy way.

    public void Form1_Load(Object sender, EventArgs e) 
    {
      ... // Other initialization code
      maskedTextBox1.Mask = "00/00/0000";
      maskedTextBox1.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected)
    }
    
    void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
    {
      toolTip1.ToolTipTitle = "Invalid Input";
      toolTip1.Show("We're sorry, but only digits (0-9) are allowed in dates.", maskedTextBox1, maskedTextBox1.Location, 5000);
    }
    
    
    Here is the walkthrough link http://msdn.microsoft.com/en-us/library/kkx4h3az.aspx
    • Marked as answer by Alan_chen Friday, August 20, 2010 2:55 AM
    Tuesday, August 17, 2010 10:17 AM
  •       Regex number = new Regex(@"\d");
          Match m = number.Match(textBox1.Text);
          if (m.Success)
            MessageBox.Show("Error! no numbers");
    
    
    In order to use Regex you'll need to add: using System.Text.RegularExpressions

    Noam B.
    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Tuesday, August 17, 2010 11:37 AM
    • Marked as answer by Alan_chen Friday, August 20, 2010 2:55 AM
    Tuesday, August 17, 2010 11:36 AM

All replies

  • Hi,

    maybe you want to ask all this C# stuff in a C# Forum.

    But you find a nice overview about User Input Validation at
    http://msdn.microsoft.com/en-us/library/ms229603.aspx

    With kind regards,

    Konrad

    Friday, August 13, 2010 11:55 AM
  • Hi, I want to write the code for validation of text box which accept only characters, no numeric values, if user will type numeric values in text box it should show errror...........what code i will write for this................................?

    Actually i m working on my project Which is related to Hotel Management.

    You are in the wrong forum, this is for the SMALL BASIC programming language which is a language aimed at beginners. 

    But for your problem you have a few ways to solve it. You can use a Masked Textbox which is set to only take characters or you could use the textbox keypress events which get called on keypress then if the character code is invalid you can just ignore it. But I haven't used C#/VB.NET for ages and really can't remember how to actually do this so for a more specific answer try the C# forums.

    • Proposed as answer by _msanders_ Friday, August 13, 2010 11:58 AM
    Friday, August 13, 2010 11:58 AM
  • Hi GNIIT,

     

    Welcome to MSDN forums!

     I am interested in helping.

    I write a sample code, hope these help.

    private void textBox3_TextChanged(object sender, EventArgs e)

            {

                string context = this.textBox3.Text;

                bool isnum = false;

                for (int i = 0; i < context.Length;i++ )

                {

                    if (char.IsNumber(context[i]))

                    {

                        isnum = true;

                        break;

                    }

                }

                if (isnum)

                {

                    this.textBox3.Text = "error types";

                }

            }

     

    Hope these helps, if you have any problems, please feel free to let me know.

     

    Best Regards,

    Alan Chen

    ________________________________________

    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Alan_chen Friday, August 20, 2010 2:55 AM
    Tuesday, August 17, 2010 7:56 AM
  • You can do this with MaskedTextbox, which is an easy way.

    public void Form1_Load(Object sender, EventArgs e) 
    {
      ... // Other initialization code
      maskedTextBox1.Mask = "00/00/0000";
      maskedTextBox1.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected)
    }
    
    void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
    {
      toolTip1.ToolTipTitle = "Invalid Input";
      toolTip1.Show("We're sorry, but only digits (0-9) are allowed in dates.", maskedTextBox1, maskedTextBox1.Location, 5000);
    }
    
    
    Here is the walkthrough link http://msdn.microsoft.com/en-us/library/kkx4h3az.aspx
    • Marked as answer by Alan_chen Friday, August 20, 2010 2:55 AM
    Tuesday, August 17, 2010 10:17 AM
  •       Regex number = new Regex(@"\d");
          Match m = number.Match(textBox1.Text);
          if (m.Success)
            MessageBox.Show("Error! no numbers");
    
    
    In order to use Regex you'll need to add: using System.Text.RegularExpressions

    Noam B.
    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Tuesday, August 17, 2010 11:37 AM
    • Marked as answer by Alan_chen Friday, August 20, 2010 2:55 AM
    Tuesday, August 17, 2010 11:36 AM