MaskedTextInput Validation Problem

4 posts, 0 answers
  1. Christopher
    Christopher avatar
    5 posts
    Member since:
    Mar 2015

    Posted 29 May 2015 Link to this post

    I have a RadMaskedTextInput Box that I have validated in the backend as follows.

    //Phone Number
    [RegularExpression(@"^\d{10}$")]
    public string PhoneNumber
    {
        get { return _phoneNumber; }
        set
        {
            double.TryParse(value, out temp);
            if (_phoneNumber != value)
            {
                Validator.ValidateProperty(value,
                    new ValidationContext(this, null, null)
                    {
                        MemberName = "PhoneNumber"
                    });
                _phoneNumber = value;
                OnPropertyChanged("PhoneNumber");
            }
        }
    }

    The xaml for the object is as follows:

    <!-- Phone Number -->
    <telerik:RadMaskedTextInput Margin="2" BorderBrush="LightGray"
                                    IsClearButtonVisible="False"
                                    Mask="(###) ###-####"
                                    UpdateValueEvent="PropertyChanged"
                                    TextMode="PlainText"
                                    Value="{Binding Path=PhoneNumber,
                                                    Mode=TwoWay,
                                                    NotifyOnValidationError=True,
                                                    ValidatesOnExceptions=True,
                                                    UpdateSourceTrigger=PropertyChanged}">

    This does not have the expected behavior.

    What I want is that when the user does not have a valid 10 digit phone number then set the validation flag that the number is incorrect.

    While it may be left blank, as soon as they start typing I want it to show a red box until only 10 digits are present.

  2. Milena
    Admin
    Milena avatar
    201 posts

    Posted 01 Jun 2015 Link to this post

    Hello Christopher,

    Thank you for the code-snippet provided.

    So, to achieve this you should set AllowInvalidValues property of the MaskedTextInput to true: 
    <telerik:RadMaskedTextInput   AllowInvalidValues="True" .../>

    This ensures that the Setter of the property will be fired even if the DataAnnotaion Validation does not succeed - in your case the error message will be shown when the users start typing until they entered all 10 digits.

    I hope this helps.

    Regards,
    Milena
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Christopher
    Christopher avatar
    5 posts
    Member since:
    Mar 2015

    Posted 01 Jun 2015 in reply to Milena Link to this post

    That worked perfectly. Thank you very much! I attempted to find documentation for every Property within the Telerik WPF controls but was unable to.
  5. Milena
    Admin
    Milena avatar
    201 posts

    Posted 02 Jun 2015 Link to this post

    Hi Christopher,

    I'm glad that the solution works for your scenario.

    About the resources - in our online documentation you can find more about the Validation and the MaskedInput control and also you can take a look at our API Reference if you are interested.

    I hope this will get you started.

    Regards,
    Milena
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top