Validation with the Masked TextBox

4 posts, 1 answers
  1. terry
    terry avatar
    16 posts
    Member since:
    Nov 2008

    Posted 02 Mar 2011 Link to this post

    I'm trying to get validation working with the RadMaskedTextBox, I have found a few posts and an old example in your online help http://www.telerik.com/help/wpf/radmaskedinput-features-validation.html

    The current help for the RadMaskedTextBox doesn't even talk about validation.

    What I would like to do is have the outline around the RadMaskedTextBox turn red when something is wrong the data entered into the textbox.

    Could you please send me a small example of this ?
  2. Answer
    Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 03 Mar 2011 Link to this post

    Hi Terry,

    Please take a look at the sample code bellow.

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:local="clr-namespace:WpfApplication1"
            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" Title="MainWindow"
            Height="350" Width="525">
        <Grid>
            <telerik:RadMaskedTextBox Width="200" Height="30" MaxLength="10" MaskType="None">
                <telerik:RadMaskedTextBox.Value>
                    <Binding Path="Name" UpdateSourceTrigger="PropertyChanged"
                            NotifyOnValidationError="True" ValidatesOnExceptions="True">
                        <Binding.ValidationRules>
                            <local:RegexValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </telerik:RadMaskedTextBox.Value>
            </telerik:RadMaskedTextBox>
        </Grid>
    </Window>

    using System.Globalization;
    using System.Windows;
    using System.Windows.Controls;
     
    namespace WpfApplication1
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
     
                this.DataContext = new DataItem();
            }
        }
     
        public class DataItem
        {
            public string Name { get; set; }
        }
     
        public class NameValidationRule : ValidationRule
        {
            public override ValidationResult Validate(object value, CultureInfo ultureInfo)
            {
                if (value.ToString().Length > 5)
                {
                    return new ValidationResult(false, "Error. String cannot be longer than 5 characters.");
                }
                else
                {
                    return new ValidationResult(true, null);
                }
            }
        }
    }

    I hope this helps.

    Best wishes,
    Kiril Stanoev
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. UI for WPF is Visual Studio 2017 Ready
  4. terry
    terry avatar
    16 posts
    Member since:
    Nov 2008

    Posted 05 Mar 2011 Link to this post

    Thanks Kiril

    Your example was very helpful.

    In the method Validate, there is error message "Error. String cannot be longer than 5 characters." When there is a validation error how can I get this error message displayed ?

    I took the example from your previous post and turned it into a demo, but the error string is never displayed.

    I looked through the Examples or Demo project that is included with the controls, at the Grid Validation example and when there is a validation error, at the top right corner of the masked textbox there is an area that if I hover with the mouse the error message will be displayed, how can I get that to work in the example that you provided me ?

    Thanks for your help
  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 08 Mar 2011 Link to this post

    Hi terry,

    You can find a possible approach for the desired behavior in the attached solution. It basically follows this article. However, we highly suggest you to use the new RadMaskedInput suite of controls. They will be officially released with the telerik Q1 2011 Release scheduled for the next week.

    All the best,
    Petar Mladenov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Back to Top