title: Validate decimal numbers in a WPF TextBox
body: <p>I want to implement decimal number validation on WPF TextBoxes.</p>
  
  <p>I found an answer for <a href="https://stackoverflow.com/questions/13251506/wpf-text-box-validation-on-numeric-value">WPF TextBox validation on numeric values</a>, but I want a TextBox that allows decimal values (float/double) as well.</p>
  
  <p>How can I do this?</p>
  
  <pre><code>public static bool GetIsDecimal(DependencyObject obj)
      {
          return (bool)obj.GetValue(IsDecimalProperty);
      }
  
      public static void SetIsDecimal(DependencyObject obj, bool value)
      {
          obj.SetValue(IsDecimalProperty, value);
      }
  
      public static readonly DependencyProperty IsDecimalProperty =
   DependencyProperty.RegisterAttached("IsDecimal", typeof(bool), typeof(TextBoxHelpers), new PropertyMetadata(false, new PropertyChangedCallback((s, e) =&gt;
   {
       TextBox targetTextbox = s as TextBox;
       if (targetTextbox != null)
       {
           if ((bool)e.OldValue &amp;&amp; !((bool)e.NewValue))
           {
               targetTextbox.PreviewTextInput -= targetTextbox_PreviewTextInput;
  
           }
           if ((bool)e.NewValue)
           {
               targetTextbox.PreviewTextInput += targetTextbox_PreviewTextInput;
               targetTextbox.PreviewKeyDown += targetTextbox_PreviewKeyDown;
           }
       }
   })));
  
      static void targetTextbox_PreviewKeyDown(object sender, KeyEventArgs e)
      {
          e.Handled = (e.Key == Key.Space);
      }
  
      static void targetTextbox_PreviewTextInput(object sender, TextCompositionEventArgs e)
      {
          Char newChar = e.Text.ToString()[0];
          e.Handled = !(Char.IsNumber(newChar) || (newChar == '.'));
      }
  }
  </code></pre>
  
  <p>I am using the above code. The code <code>(Char.IsNumber(newChar) || (newChar == '.'))</code> will check for numbers and decimals. So now the TextBox allows only numbers and decimal(.). But the problem is that, I can enter multiple decimal points (For example, 1.01.22.011). So I want to restrict entering multiple decimal points.</p>