locked
Validating TextBox Control Content

    Question

  • Hi, 

    I have the following code to validate the content contained within two TextBox controls: 

            private void CaseValidation_KeyUp(object sender, KeyRoutedEventArgs e)
            {
                bool isValidCaseName, isValidCaseNum;
                if (CaseNameEntry.Text != null)
                    isValidCaseName = true;
                else
                    isValidCaseName = false;
    
                if (CaseNumberEntry.Text != null)
                {
                    try
                    {
                        int caseNum = Int32.Parse(CaseNumberEntry.Text);
                        isValidCaseNum = true;
                    }
                    catch (FormatException)
                    {
                        isValidCaseNum = false;
                    }
                }
                else
                    isValidCaseNum = false;
    
                if(isValidCaseName == true && isValidCaseNum == true)
                {
                    SiteSaveBtn.IsEnabled = true;
                }
            }

    The code works great for validating the data, except it fails at one point. If I entry the appropriate data into two fields, the save button becomes available, but then if I delete the data from one or both fields (making it invalid), the save button does not disappear. 

    How can I make this revalidate on each KeyUp event for either of the fields? Is this the best way to handle this validation? 

    Thanks.

    Friday, June 27, 2014 3:55 PM

Answers