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

  • Home
  • SEARCH
  • 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 8894841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:36:37+00:00 2026-06-14T23:36:37+00:00

I am using a ValueConverter to display double types as currency. I want to

  • 0

I am using a ValueConverter to display double types as currency. I want to make sure the entry is valid, so I tried using this validation method I made up. My only problem seems to be that I can’t select the whole value (ie $500.00) and type anything. If the ‘$’ is not selected, everything seems fine. How can I fix this?

XAML:

<TextBox Text="{Binding Converter={StaticResource MoneyConverter}, Path=fr}"
  PreviewTextInput="ValidateMoney" DataObject.Pasting="TextBox_Pasting"/>

Code behind:

private void ValidateMoney(object sender, TextCompositionEventArgs e) {
  Regex rgx = new Regex(@"^(\$)[0-9]*[.]{0,1}[0-9]*$|^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");
  e.Handled = !rgx.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart, e.Text));
}
public class MoneyConverter : IValueConverter {
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    if (value != null) {
      double d;
      string temp = value.ToString();
      if (double.TryParse(temp, out d)) {
        return String.Format("{0:C}", d);
      } else {
        return value;
      }
    } else {
      return value;
    }
  }
  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
    if (value != null) {
      double d;
      string temp = value.ToString();
      if (double.TryParse(temp, out d)) {
        return String.Format("{0:C}", d);
      } else {
        return value;
      }
    } else {
      return value;
    }
  }
}
  • 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-14T23:36:39+00:00Added an answer on June 14, 2026 at 11:36 pm

    Your approach to including the dollar sign is correct, though the parentheses are unnecessary. It is sufficient to write this: ^\$.

    You can simplify your regular expression a lot:

    • You don’t need three separate cases in your alternation. There is some unnecessary repetition there.
    • {0,1} can be replaced with ?.
    • You probably want to ensure that there is at least one digit.

    Try this:

    @"^\$?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)$"
    

    Or using capturing groups to reduce clutter:

    @"^\$?([0-9]+(\.[0-9]+)?|\.[0-9]+)$"
    

    Some other points:

    • You should be aware that TryParse is affected by the current culture. 1.990 means different things depending on your culture.
    • ToString also depends on culture. The "{0:C}" format doesn’t guarantee a dollar sign.
    • Your regular expression allows things like $.990. That might be what you want, but it looks a little odd to me.
    • You should probably use decimal rather than double to store monetary amounts. The double type can’t store 1.99 exactly, but decimal can.
    • I replaced [.] with the equivalent \. but that is just a matter of taste. Both do the same thing.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using a populated Table Type as the source for a TSQL-Merge. I want to
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using CI for the first time and i'm smashing my head with this seemingly
I have this legacy database for which I'm building a custom viewer using Linq
I want to try out a small, custom ValueConverter in a class which after
I am using a RSS feed to display content in my app. Everything works
Silverlight validation using INotifyDataErrorInfo was working great with its slick error displays until I
I'm using a boolean converter in my XAML in a WPF project. I want
I want to set the BorderThickness of a Border of a UserControl using 4
I want to make slider to select dates. For example, every hour in last

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.