Home > database >  Regex expression giving false value even when correcting the input
Regex expression giving false value even when correcting the input

Time:02-24

I wrote a regex expression to make sure that the user is entering the correct format in the textboxes, if the user entered a wrong value a warning will appear, The problem is: when correcting the input to the right format it will still show the warning statement and won't go to the other form. And I don't understand why.

 if (txtFirstName.Text.Length == 0 || txtLastName.Text.Length == 0 || txtEmail.Text.Length == 0 || txtEmail.Text.Length == 0 || txtPassword.Text.Length == 0)
            {
                MessageBox.Show("Make sure to fill All text boxes!");
            }
            else
            {
                if (Regex.IsMatch(txtFirstName.Text, @"^[a-zA-Z]$"))
                {
                    MainMenu mainMenu = new MainMenu();
                    mainMenu.Show();
                    this.Hide();
                }
                else
                {
                    lblWarning.Visible = true;
                    txtFirstName.Text = " ";
                }

                if (Regex.IsMatch(txtLastName.Text, @"^[a-zA-Z]$"))
                {
                    MainMenu mainMenu = new MainMenu();
                    mainMenu.Show();
                    this.Hide();
                }
                else
                {
                    lblWarning2.Visible = true;
                    txtLastName.Text = " ";
                }

                if (Regex.IsMatch(txtEmail.Text, @"^[a-zA-z]{1-10}@(gmail|hotmail|cloud).(com|org)$"))
                {
                    MainMenu mainMenu = new MainMenu();
                    mainMenu.Show();
                    this.Hide();
                }
                else
                {
                    lblWarning3.Visible = true;
                    txtEmail.Text = " ";
                }

                if (Regex.IsMatch(txtPhoneNumber.Text, @"^[0-9]{10}$"))
                {
                    MainMenu mainMenu = new MainMenu();
                    mainMenu.Show();
                    this.Hide();
                }
                else
                {
                    lblWarning4.Visible = true;
                    txtPhoneNumber.Text = " ";
                }

CodePudding user response:

Regex.IsMatch(txtFirstName.Text, @"^[a-zA-Z] $")

your regex will only match txtFirstName consisting of one letter.

CodePudding user response:

Change your regex from

@"^[a-zA-z]{1-10}@(gmail|hotmail|cloud).(com|org)$"

to

@"^[a-zA-z]{1,10}@(gmail|hotmail|cloud)\.(com|org)$"

  • Related