Mark Fenech

Mark Fenech

  • NA
  • 82
  • 69k

C# Validation

May 9 2011 10:08 AM
I am validating some textboxes, so when i press button if validation is ok itt will do its act this is my code dont know why it isnt working. anyhelp please:

   private void button1_Click(object sender, EventArgs e)
        {
                if (!Regex.IsMatch(Username_txt.Text, @"^[a-zA-Z]{1,25}$") && Username_txt.Text.Length > 25 && (Username_txt.Text == String.Empty))
                {
                    MessageBox.Show("Error in username");
                }
                else if  (!Regex.IsMatch(name_txt.Text, @"^[a-zA-Z]{1,25}$") && name_txt.Text.Length > 25 && (name_txt.Text == String.Empty))
                {
                    MessageBox.Show("Error in surname");
                }
                else if (!Regex.Match(email_txt.Text, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*").Success)
                {
                    MessageBox.Show("Incorrect Email Address");
                }
                else if (remail_txt.Text != email_txt.Text)
                {
                    MessageBox.Show("Emails do not match");
                }
                else if (!Regex.IsMatch(pass_txt.Text, @"^[a-zA-Z]{1,25}$") && pass_txt.Text.Length > 25 && pass_txt.Text == String.Empty)
                {
                    MessageBox.Show("Incorrect password");
                }
                else
                {
                    db.registerUser(Username_txt.Text, pass_txt.Text, name_txt.Text, surname_txt.Text, email_txt.Text, rMale, rFemale);
                    MessageBox.Show("Data Submitted", "Registration Form Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Form1 fm = new Form1();
                    this.Hide();
                    fm.Show();
                }
        }

Answers (2)