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.

[VB.NET] Example 1: Custom validation class

Public Class DataValidationViewModel
Inherits ViewModelBase
Private m_doubleValue As Double
Private m_stringValue As String
Private m_decimalValue As Decimal
Private m_dateTimeValue As Date
Public Property DoubleValue() As Double
Get
Return m_doubleValue
End Get
Set
If Value < -100 OrElse Value > 100 Then
Throw New ValidationException([String].Format("Value is {0} than {1}.", (If(Value > 100, "greater", "less")), Value))
End If
m_doubleValue = Value
Me.OnPropertyChanged("DoubleValue")
End Set
End Property
Public Property DecimalValue() As Decimal
Get
Return m_decimalValue
End Get
Set
If Value < -100 OrElse Value > 100 Then
Throw New ValidationException([String].Format("Value is {0} than {1}.", (If(Value > 100, "greater", "less")), Value))
End If
m_decimalValue = Value
Me.OnPropertyChanged("DecimalValue")
End Set
End Property
Public Property DateTimeValue() As Date
Get
Return m_dateTimeValue
End Get
Set
If Value < Date.Parse("01.01.1900") OrElse Value > Date.Parse("01.01.2099") Then
Throw New ValidationException([String].Format("Value is {0} than {1}.", (If(Value > Date.Parse("01.01.2099"), "greater", "less")), Value))
End If
m_dateTimeValue = Value
Me.OnPropertyChanged("DateTimeValue")
End Set
End Property
Public Property StringValue() As String
Get
Return m_stringValue
End Get
Set
If Value.Length > 6 Then
Throw New ValidationException([String].Format("Value's length is greater than {0}.", Value.Length))
End If
m_stringValue = Value
Me.OnPropertyChanged("StringValue")
End Set
End Property
End Class

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.