none
C#: TextBox Validation

    Question

  • Hi,

    I created a Windows Application and I want to know how can I validate the textbox im using that it will not accept the special character " | " and to limit character input to 100?

    and also to set another textbox that it will only allow time format like this hh:mm AM/PM and that it will tell me once I try to move to another textbox that my input is invalid?

    Hope someone can help me with this.

    Wednesday, July 17, 2013 2:25 AM

Answers

  • Hi,MukuroRokudo

    You can use the Leave event of the TextBox instead of the Click event of Button.like this

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.textBox1.Leave+=textBox1_Leave;
                this.textBox2.Leave += textBox2_Leave;
            }
            private void textBox1_Leave(object sender, EventArgs e)
            {
                string valEx = @"(0\d|1[12]):(00|00) (AM|PM)";
                if (!Regex.IsMatch(this.textBox1.Text.Trim(), valEx))
                {
                    MessageBox.Show("不合法!");
                }
            }
            void textBox2_Leave(object sender, EventArgs e)
            {
                string reg = @"^((\d|[123456789]\d)(\.\d+)?|100)$";
                if (!Regex.IsMatch(this.textBox2.Text.Trim(), reg))
                {
                    MessageBox.Show("不合法!");
                }
            }
    
          
        }

    Regards,

    buernuo


    ALL FOR ONE AND ONE FOR ALL.

    Wednesday, July 17, 2013 3:53 AM
  • Hi,MukuroRokudo

    I'm sorry for replying it so late  ,you can check e.KeyChar like this

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { // You can set a breakpoint in debug mode to get the KeyChar of "|"

    //You can search the KeyChar of "|" in the browser

    if (e.KeyChar==124) { e.Handled = true; } }

    Best Regards


    ALL FOR ONE AND ONE FOR ALL.

    Thursday, July 18, 2013 1:28 AM

All replies

  • Hi,MukuroRokudo

    You can use Regex Class to solve it . As I understanding,I give you a solution like this

            private void button1_Click(object sender, EventArgs e)
            {
                string valEx = @"(0\d|1[12]):(00|00) (AM|PM)";
                if (!Regex.IsMatch(this.textBox1.Text.Trim(), valEx))
                {
                    MessageBox.Show("不合法!");
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                string reg = @"^((\d|[123456789]\d)(\.\d+)?|100)$";
                if (!Regex.IsMatch(this.textBox2.Text.Trim(), reg))
                {
                    MessageBox.Show("不合法!");
                }
            }

    I hope it can help u! If it isn't what you need,please let me know.

    Best Regards.

    buernuo


    ALL FOR ONE AND ONE FOR ALL.

    Wednesday, July 17, 2013 3:33 AM
  • Hi buernuo,

    Thanks, it seems a little near but what I need is not in button click. I need to have the messagebox to pop-up whenever I tried to press tab or enter or click my mouse, like going out of the textbox with the unwanted value entered.

    Thanks,

    MukuroRokudo

    Wednesday, July 17, 2013 3:39 AM
  • Hi,MukuroRokudo

    You can use the Leave event of the TextBox instead of the Click event of Button.like this

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.textBox1.Leave+=textBox1_Leave;
                this.textBox2.Leave += textBox2_Leave;
            }
            private void textBox1_Leave(object sender, EventArgs e)
            {
                string valEx = @"(0\d|1[12]):(00|00) (AM|PM)";
                if (!Regex.IsMatch(this.textBox1.Text.Trim(), valEx))
                {
                    MessageBox.Show("不合法!");
                }
            }
            void textBox2_Leave(object sender, EventArgs e)
            {
                string reg = @"^((\d|[123456789]\d)(\.\d+)?|100)$";
                if (!Regex.IsMatch(this.textBox2.Text.Trim(), reg))
                {
                    MessageBox.Show("不合法!");
                }
            }
    
          
        }

    Regards,

    buernuo


    ALL FOR ONE AND ONE FOR ALL.

    Wednesday, July 17, 2013 3:53 AM
  • Hi buernuo,

    I have tried the code you provided but apparently trying the one for not to accept " | ", it seems whenever I tried to move out of the textbox the MessageBox always pop-ups. I can't do a thing after even I'm not entering " | ".

    See my sample code below

    namespace Sample
    {
        public partial class AddPage : Form
        {
            public AddPage()
            {
                InitializeComponent();
                this.FormClosing += new FormClosingEventHandler(AddPage_FormClosing);
                this.descriptionTextBox.Leave += descriptionTextBox_Leave;
            } 

    private void descriptionTextBox_Leave(object sender, EventArgs e)
            {
                string reg = @"^((\d|[123456789]\d)(\.\d+)?|100)$";
                if (!Regex.IsMatch(this.descriptionTextBox.Text.Trim(), reg))
                {
                    MessageBox.Show("不合法!");
                }
            }
       }
    }

    Thanks,

    MukuroRokudo

    Wednesday, July 17, 2013 4:49 AM
  • Hi,

    I saw how to limit the textbox to 100. I just set the MaxLenght of the textbox property :).

    All I need now is to not allow the textbox to accept the " | " and that it will warn me if I used the " | "

    Thanks,

    MukuroRokudo

    Wednesday, July 17, 2013 5:21 AM
  • Hi,MukuroRokudo

    I'm sorry for replying it so late  ,you can check e.KeyChar like this

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { // You can set a breakpoint in debug mode to get the KeyChar of "|"

    //You can search the KeyChar of "|" in the browser

    if (e.KeyChar==124) { e.Handled = true; } }

    Best Regards


    ALL FOR ONE AND ONE FOR ALL.

    Thursday, July 18, 2013 1:28 AM