Im trying to write c# form validation for a xaml-form. The only two things that seem to work are the lines of code that clears the text- and passwordboxes and displays the messagebox. Are my regex-examples wrong, or do I need to work with some kind of while-statement(s)?
//start with capital letter
Match RegMatchFirstName = Regex.Match(TextBoxFirstName.Text, "^[A-Z]+[a-zA-Z]*$");
if (!RegMatchFirstName.Success)
{
TxtFirstNameWrong2.Visibility = Visibility.Collapsed;
TextBoxFirstName.Text = "";
}
else
TxtFirstNameWrong2.Visibility = Visibility.Visible;
Match RegMatchFirstNameLength = Regex.Match(TextBoxFirstName.Text, "^{1-20}$");
if (!RegMatchFirstNameLength.Success)
{
TxtFirstNameWrong.Visibility = Visibility.Collapsed;
TextBoxFirstName.Text = "";
}
else
TxtFirstNameWrong.Visibility = Visibility.Visible;
//start with capital letter
Match RegMatchLastName = Regex.Match(TextBoxLastName.Text, "^[A-Z]+[a-zA-Z]*$");
if (!RegMatchFirstName.Success)
{
TxtLastNameWrong2.Visibility = Visibility.Collapsed;
TextBoxLastName.Text = "";
}
else
TxtLastNameWrong2.Visibility = Visibility.Visible;
Match RegMatchLastNameLength = Regex.Match(TextBoxLastName.Text, "^{1-20}$");
if (!RegMatchLastNameLength.Success)
{
TxtLastNameWrong.Visibility = Visibility.Collapsed;
TextBoxLastName.Text = "";
}
else
TxtLastNameWrong.Visibility = Visibility.Visible;
//18-45
Match RegMatchAge = Regex.Match(Convert.ToString(TextBoxAge.Text), "^([1][8-9]|[2-3][0-9]|[4][0-5])$");
if (!RegMatchAge.Success)
{
TxtAgeReq.Visibility = Visibility.Collapsed;
TextBoxAge.Text = "";
}
else
TxtAgeReq.Visibility = Visibility.Visible;
Match RegMatchUsername = Regex.Match(TextBoxUsername.Text, "^{1-10}$");
if (!RegMatchUsername.Success)
{
TxtUsernameWrong.Visibility = Visibility.Collapsed;
TextBoxUsername.Text = "";
}
else
TxtUsernameWrong.Visibility = Visibility.Visible;
Match RegMatchPassword = Regex.Match(PBoxPW.ToString(), "^{6-10}$");
if (!RegMatchUsername.Success)
{
TxtPasswordWrong.Visibility = Visibility.Collapsed;
PBoxPW.Password = "";
}
else
TxtPasswordWrong.Visibility = Visibility.Visible;
if (PBoxRepeatPW != PBoxPW)
{
TxtRepeatPasswordWrong.Visibility = Visibility.Collapsed;
PBoxRepeatPW.Password = "";
}
else
TxtRepeatPasswordWrong.Visibility = Visibility.Visible;
Match RegMatchEmail = Regex.Match(TextBoxEmail.Text, @"^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$");
if (!RegMatchEmail.Success)
{
TxtEmailWrong.Visibility = Visibility.Collapsed;
TextBoxEmail.Text = "";
}
else
TxtEmailWrong.Visibility = Visibility.Visible;
MessageBox.Show("Membership registered.");
/newbie, thankful for help