I have an XML-form (Silverlight application) with validations in C#. My XML-code works fine, but I'm having problems with the C# part.

For every textbox I first check that the textbox is not empty (this works fine), and if it's not empty I check that the input is correct. My problem is that the program reacts as if the input is wrong (the error messages shows) even when it's correct.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions; 


namespace InlamningSL
{
	public partial class Page : UserControl
	{
        	public Page()
            {
                // Required to initialize variables
                InitializeComponent();
            }


        private void ButtonSend_Clicked(object sender, RoutedEventArgs e)
        {
            //FIRST NAME   
            if (String.IsNullOrEmpty(TextBoxFirstName.Text))
            {
                TxtFirstNameReq.Visibility = Visibility.Visible;
            }
            else
            {
                //only letters, max length 20
                Match RegMatchFirstName = Regex.Match(TextBoxFirstName.Text, "[A-Z]+[a-zA-Z]{1-20}");

                if (!RegMatchFirstName.Success)
                {
                    TxtFirstNameWrong.Visibility = Visibility.Visible;
                }
            }

            //LAST NAME
            if (String.IsNullOrEmpty(TextBoxLastName.Text))
            {
                TxtLastNameReq.Visibility = Visibility.Visible;
            }
            else
            {
                //only letters, max length 20
                Match RegMatchLastName = Regex.Match(TextBoxLastName.Text, "[A-Z]+[a-zA-Z]{1-20}");

                if (!RegMatchLastName.Success)
                {
                    TxtLastNameWrong.Visibility = Visibility.Visible;
                }
            }

            //AGE
            if (String.IsNullOrEmpty(TextBoxAge.Text))
            {
                TxtAgeReq.Visibility = Visibility.Visible;
            }
            else
            {
                //allowed age 18-45
                Match RegMatchAge = Regex.Match(Convert.ToString(TextBoxAge.Text), "([1][8-9]|[2-3][0-9]|[4][0-5])");  //**Doesn't work**

                if (!RegMatchAge.Success)
                {
                    TxtAgeWrong.Visibility = Visibility.Visible;
                }
            }

            //USERNAME  
            if (String.IsNullOrEmpty(TextBoxUsername.Text))
            {
                TxtUsernameReq.Visibility = Visibility.Visible;
            }
            else
            {
                //any signs, max length 10
                Match RegMatchUsername = Regex.Match(TextBoxUsername.Text, "{1-10}");

                if (!RegMatchUsername.Success)
                {
                    TxtUsernameWrong.Visibility = Visibility.Visible;
                }
            }

            //PASSWORD
            if (String.IsNullOrEmpty(PBoxPW.Password.ToString()))
            {
                TxtPasswordReq.Visibility = Visibility.Visible;
            }
            else
            {
                //any signs, allowed length 6-10
                Match RegMatchPassword = Regex.Match(PBoxPW.ToString(), "{6-10}");

                if (!RegMatchPassword.Success)
                {
                    TxtPasswordWrong.Visibility = Visibility.Visible;
                }
            }

            //REPEAT PASSWORD
            if (String.IsNullOrEmpty(PBoxRepeatPW.Password.ToString()))
            {
                TxtRepeatPasswordReq.Visibility = Visibility.Visible;
            }
            else
            {
                //repeat password validation
                if (PBoxRepeatPW != PBoxPW)
                {
                    RepeatPasswordWrong.Visibility = Visibility.Visible;
                }
            }

            //E-MAIL
            if (String.IsNullOrEmpty(TextBoxEmail.Text))
            {
                TxtEmailReq.Visibility = Visibility.Visible;
            }
            else
            {
            //e-mail validation
            Match RegMatchEmail = Regex.Match(TextBoxEmail.Text, @"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b");

            if (!RegMatchEmail.Success)
            {
                TxtEmailWrong.Visibility = Visibility.Visible;
            }
            }
        }
        }
}
This article has been dead for over six months. Start a new discussion instead.