Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8457011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:41:49+00:00 2026-06-10T12:41:49+00:00

I have a WPF textbox that is databound. I need to restrict the user

  • 0

I have a WPF textbox that is databound. I need to restrict the user input on the textbox so that it only accepts numbers and a single period (For displaying decimals).

I know I can handle this in “Winforms” way and validate each input on KeyPress event, but I was wondering if there was a cleaner and maybe even proper way to do this in WPF (especially since I am databinding the textbox).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:41:51+00:00Added an answer on June 10, 2026 at 12:41 pm

    Use ValidationRules provided by WPF.

    The xaml would be:

    <TextBox>
        <TextBox.Text>
            <Binding Path="Name">
                <Binding.ValidationRules>
                    <ExceptionValidationRule />
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    

    The code for the textbox property would be (used regex for the validation):

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            if (!Regex.IsMatch(value, @"^((?:[1-9]\d*)|(?:(?=[\d.]+)(?:[1-9]\d*|0)\.\d+))$"))
            {
                throw new ApplicationException("Please enter only numbers/decimals.");
            }
        }
    }
    

    Source: Validation in WPF


    The regex given above: ^((?:[1-9]\d*)|(?:(?=[\d.]+)(?:[1-9]\d*|0)\.\d+))$ can be tested at this Rubular link

    The regex would match these:

    1.2
    22522
    0.33
    3.90000
    

    but not these: (you could tweak the regex to allow some of them)

    .999
    23.35.1343
    03423.23423
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF textBox that is declared as ReadOnly <TextBox IsReadOnly=True IsTabStop=False Width=200
I have a WPF TextBox in which I want to allow autocompleting user names
I have a WPF form with a combobox and a textbox (both are databound
I have an editable WPF ComboBox with TextSearchEnabled. I need to force the user's
I'm writing a WPF calculator app. In this application I have TextBox that get
I need a WPF Textbox that displays a phone number as (555) 555-5555 but
I have a simple control that extends the WPF TextBox control. The basic idea
I have a TextBox that user can enter search-term in it. Its bind to
I have a WPF Window that contains a TextBox. I have implemented a Command
I have an WPF usercontrol that has a TextBox. I set the text Underline

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.