Throwing Validation Exceptions
Masks do not necessarily guarantee that a user's input will represent a valid value for a given type; for example, -9 could be entered for an age in years. You can verify that a user's input represents a valid value by using Data Validation. The RadMaskedInput controls provides out-of-the-box support for Data Validation. Data validation, being one of the major points when building line-of-business applications, can help you to easily separate the validation logic from the application's UI.
This topic will show you how to use RadMaskedInput controls to distinguish between proper and improper user input.
For the purpose of this tutorial you need to create a new class named DataValidationViewModel. It will take care of the validation rules.
Example 1: Custom validation class
public class DataValidationViewModel : ViewModelBase
{
private double doubleValue;
private string stringValue;
private decimal decimalValue;
private DateTime dateTimeValue;
[Range(-100d, 100d)]
public double DoubleValue
{
get { return doubleValue; }
set
{
doubleValue = value;
this.OnPropertyChanged("DoubleValue");
}
}
[Range(typeof(decimal), "-100", "100")]
public decimal DecimalValue
{
get { return decimalValue; }
set
{
decimalValue = value;
this.OnPropertyChanged("DecimalValue");
}
}
[Range(typeof(DateTime), "01.01.1900", "01.01.2099")]
public DateTime DateTimeValue
{
get { return dateTimeValue; }
set
{
dateTimeValue = value;
this.OnPropertyChanged("DateTimeValue");
}
}
[StringLength(6)]
[Required()]
public string StringValue
{
get { return stringValue; }
set
{
stringValue = value;
this.OnPropertyChanged("StringValue");
}
}
}Declare several RadMaskedInput controls in XAML for displaying the sample data. Pay attention on the following things:
-
The RadMaskedInput controls' Value property is data bound to the corresponding property in the ViewModel.
-
The ValidatesOnExceptions and NotifyOnValidationError properties of the data binding are set to True.
Example 2: Defining several RadMaskedInput controls in XAML
<StackPanel x:Name="LayoutRoot" Background="White">
<telerik:RadMaskedNumericInput Width="200"
Margin="10, 10, 10, 0"
ErrorMessage="{Binding Path=Text, ElementName=customErrorMessage}"
InputBehavior="Insert"
Value="{Binding Path=DoubleValue, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />
<telerik:RadMaskedCurrencyInput Width="200"
Margin="10, 10, 10, 0"
ErrorMessage="{Binding Path=Text, ElementName=customErrorMessage}"
InputBehavior="Insert"
Value="{Binding Path=DecimalValue, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />
<telerik:RadMaskedDateTimeInput Width="200"
Margin="10, 10, 10, 0"
ErrorMessage="{Binding Path=Text, ElementName=customErrorMessage}"
Value="{Binding Path=DateTimeValue, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />
<telerik:RadMaskedTextInput Width="200"
Margin="10"
ErrorMessage="{Binding Path=Text, ElementName=customErrorMessage}"
Value="{Binding Path=StringValue, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />
</StackPanel>
Finally, set the DataValidationViewModel to the UserControl's DataContext property.
Example 3: Setting the DataContext to DataValidationViewModel
this.DataContext = new DataValidationViewModel();Run the demo and fill all fields with valid data. As you can see no validation errors occur.

Next try to fill some improper data.
