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 8197155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:41:03+00:00 2026-06-07T05:41:03+00:00

My problem in a nutshell: I have a WPF TextBox where a user should

  • 0

My problem in a nutshell:

I have a WPF TextBox where a user should only be allowed to enter decimal numbers. I have my validation logic in my TextBox’s PreviewTextInput event handler.

So fare I have tried:

Using TryParse

double number = 0;
bool isSuccessful = double.TryParse(e.Text, out number);
e.Handled = !(number >= 0 && isSuccessful);

Using a Regex: (as per Justin Morgan’s response to this question)

string validNumberFormat = @"^[-+]?(\d{1,3}((,\d{3})*(\.\d+)?|([.\s]\d{3})*(,\d+)?)|\d+([,\.]\d+)?)$";
e.Handled  = Regex.Matches(e.Text, validNumberFormat).Count < 1;

Both of the above methods will only allow me to enter numbers, but not the single decimal point.

Any suggestions would be most welcome.

  • 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-07T05:41:04+00:00Added an answer on June 7, 2026 at 5:41 am

    is use a behavior(Blend SDK System.Windows.Interactivity) instead of handling PreviewTextInput directly. i do this because of reusability and also you should know that the space is not handled with TextInput and also Pasting is not handled. btw i think Jonathan and Felice Pollano are right, regarding to your “decimal point” problem.

    public class TextBoxInputBehavior : Behavior<TextBox>
    {
        public TextBoxInputMode InputMode { get; set; }
    
        public TextBoxInputBehavior()
        {
            this.InputMode = TextBoxInputMode.None;
        }
    
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.PreviewTextInput += AssociatedObjectPreviewTextInput;
            AssociatedObject.PreviewKeyDown += AssociatedObjectPreviewKeyDown;
    
            DataObject.AddPastingHandler(AssociatedObject, Pasting);
    
        }
    
        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.PreviewTextInput -= AssociatedObjectPreviewTextInput;
            AssociatedObject.PreviewKeyDown -= AssociatedObjectPreviewKeyDown;
    
            DataObject.RemovePastingHandler(AssociatedObject, Pasting);
        }
    
        private void Pasting(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(string)))
            {
                var pastedText = (string)e.DataObject.GetData(typeof(string));
    
                if(!this.IsValidInput(this.GetText(pastedText)))
                {
                    System.Media.SystemSounds.Beep.Play();
                    e.CancelCommand();
                }
            }
            else
            {
                System.Media.SystemSounds.Beep.Play();
                e.CancelCommand();
            }
        }
    
        private void AssociatedObjectPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                if (!this.IsValidInput(this.GetText(" ")))
                {
                    System.Media.SystemSounds.Beep.Play();
                    e.Handled = true;
                }
            }
        }
    
        private void AssociatedObjectPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!this.IsValidInput(this.GetText(e.Text)))
            {
                System.Media.SystemSounds.Beep.Play();
                e.Handled = true;
            }
        }
    
        private string GetText(string input)
        {
            var txt = this.AssociatedObject;
            var realtext = txt.Text.Remove(txt.SelectionStart, txt.SelectionLength);
            var newtext = realtext.Insert(txt.CaretIndex, input);
    
            return newtext;
        }
    
        private bool IsValidInput(string input)
        {
            switch (InputMode)
            {
                case TextBoxInputMode.None:
                    return true;
                case TextBoxInputMode.DigitInput:
                    return input.CheckIsDigit();
    
                case TextBoxInputMode.DecimalInput:
                    //minus einmal am anfang zulässig
                    if (input == "-")
                        return true;
                    decimal d;
                    return decimal.TryParse(input, out d);
                default: throw new ArgumentException("Unknown TextBoxInputMode");
    
            }
            return true;
        }
    }
    
    public enum TextBoxInputMode
    {
        None,
        DecimalInput,
        DigitInput
    }
    

    using

       <TextBox>
            <i:Interaction.Behaviors>
                <MyBehaviors:TextBoxInputBehavior InputMode="DecimalInput"/>
            </i:Interaction.Behaviors>
        </TextBox>                
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an extremely strange problem with VS2010's multi-targeting. In a nutshell, only targeting
Problem: I have a table that prints out vertical but I would like it
Problem: I have two array where one produce a category and the second produce
Problem Using Director 11.5 and Windows 7, with MouseWheel Xtra (wheelmouse.zip), I have the
I have run into an exasperating problem getting a Java service client to communicate
I have a problem with all my forms in symfony 1.4. They work with
i'm use selenium. i use it by using firefox plugin. but i have problem
I have following setup. from django.db import models from django.contrib.auth.models import User class Event(models.Model):
The problem in a nutshell is that in development mode we'd make changes to
I have a problem when using Jython, but I can't seem to find a

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.