Telerik UI for Windows Phone by Progress

This article explains how to use the validation capabilities of RadTextBox

Validation

The text in RadTextBox can be validated. The control doesn't do the validation automatically, but it can go to a different visual state if it knows that the text is valid, for example. The new visual state can be applied with the ChangeValidationState method. It has two parameters. The first is the validation state, which is of type ValidationState and can have the following values:

  • NotValidated - this is the default state when it is not clear whether the text is valid or not.
  • Validating - this is the state when the validation logic is being executed.
  • Valid - this is the state when the validation is completed and the text is valid.
  • Invalid - this is the state for the control when the text is not valid.
The second parameter in the ChangeValidationState method is ValidationMessage. This is the message that will be displayed above the text box to notify the end user about the validation. Here is an example of a text box for an email:

CopyC#
public MainPage()
{
    InitializeComponent();
    radTextBox.LostFocus += new RoutedEventHandler(radTextBox_LostFocus);
}

private void radTextBox_LostFocus(object sender, RoutedEventArgs e)
{
    radTextBox.ChangeValidationState(ValidationState.Validating, "Validating");
    bool isValid = Validate(radTextBox.Text);
    if (isValid)
    {
        radTextBox.ChangeValidationState(ValidationState.Valid, "Correct!");
    }
    else
    {
        radTextBox.ChangeValidationState(ValidationState.Invalid, "Please enter a valid email!");
    }
}

private bool Validate(string text)
{
    // Perform some validation and return true of false depending on whether the text is valid.
}

Here is how the control will look in the different states:

Text Box-Features-Validation-01