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 Silverlight Data Validation. RadMaskedTextBox provides out-of-the-box support for Silverlight 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 RadMaskedTextBox's mask 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.
CopyC#
public class DataValidationViewModel
{
private string name;
private string phone;
private DateTime hireDate;
private double salary;
public string Name
{
get
{
return name;
}
set
{
if ( String.IsNullOrEmpty( value ) )
throw new Exception( "Please provide a valid name" );
name = value;
}
}
public string Phone
{
get
{
return phone;
}
set
{
if ( String.IsNullOrEmpty( value ) ||
value.Length < 9 )
throw new Exception( "Please provide a nine digits phone" );
phone = value;
}
}
public DateTime HireDate
{
get
{
return hireDate;
}
set
{
if ( value.CompareTo( DateTime.Now ) == -1 )
throw new Exception( "Invalid data! You cannot hire on past dates" );
hireDate = value;
}
}
public double Salary
{
get
{
return salary;
}
set
{
if ( value < 0 )
throw new Exception( "Invalid salary" );
salary = value;
}
}
}
CopyVB.NET
Public Class DataValidationViewModel
Private m_name As String
Private m_phone As String
Private m_hireDate As DateTime
Private m_salary As Double
Public Property Name() As String
Get
Return m_name
End Get
Set
If [String].IsNullOrEmpty(value) Then
Throw New Exception("Please provide a valid name")
End If
m_name = value
End Set
End Property
Public Property Phone() As String
Get
Return m_phone
End Get
Set
If [String].IsNullOrEmpty(value) OrElse value.Length < 9 Then
Throw New Exception("Please provide a nine digits phone")
End If
m_phone = value
End Set
End Property
Public Property HireDate() As DateTime
Get
Return m_hireDate
End Get
Set
If value.CompareTo(DateTime.Now) = -1 Then
Throw New Exception("Invalid data! You cannot hire on past dates")
End If
m_hireDate = value
End Set
End Property
Public Property Salary() As Double
Get
Return m_salary
End Get
Set
If value < 0 Then
Throw New Exception("Invalid salary")
End If
m_salary = value
End Set
End Property
End ClassDeclare several RadMaskedTextBox controls in XAML for displaying the sample data. Pay attention on the following things:
- The RadMaskedTextBox's 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.
- The UpdateValueEvent property is set to LostFocus.
CopyXAML
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel>
<TextBlock Margin="5 0" Text="Name" />
<telerik:RadMaskedTextBox Margin="0,5,0,10"
MaskType="None"
UpdateValueEvent="LostFocus"
Value="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
<TextBlock Margin="5 0" Text="Phone" />
<telerik:RadMaskedTextBox Margin="0,5,0,10"
Mask="###-###-###"
MaskType="Standard"
UpdateValueEvent="LostFocus"
Value="{Binding Path=Phone, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
<TextBlock Margin="5 0" Text="HireDate" />
<telerik:RadMaskedTextBox Margin="0,5,0,10"
Mask="d"
MaskType="DateTime"
UpdateValueEvent="LostFocus"
Value="{Binding Path=HireDate, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
<TextBlock Margin="5 0" Text="Salary" />
<telerik:RadMaskedTextBox Margin="0,5,0,10"
Culture="bg-BG"
Mask="c3"
MaskType="Numeric"
UpdateValueEvent="LostFocus"
Value="{Binding Path=Salary, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
</StackPanel>
</Grid>Finally, set the DataValidationViewModel to the UserControl's DataContext property.
CopyC#
this.DataContext = new DataValidationViewModel();
CopyVB.NET
Me.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.
Tip |
|---|
| You can see a live demo demonstrating the DataValidation feature here. |
See Also