Question: I will like to validate user entry to ensure they are integers. How can I do it? I thought of using `IDataErrorInfo` which seems like the "correct" way to do validation in WPF. So I tried implementing it, in my ViewModel.

But the thing is my text box is bound to an integer field, and there isn't any need to validate if an `int` is an `int`. I noticed that WPF automatically adds a red border around the textbox to notify the user of the error. The underlying property doesn't change to an invalid value. But I would like to notify the user of this. How can I do it?

Answer: 
We can do validation on text box changed event.
The following implementation prevents keypress input other than numeric and one decimal point.

```
private void textBoxNumeric_TextChanged(object sender, TextChangedEventArgs e)
{
        TextBox textBox = sender as TextBox;
        Int32 selectionStart = textBox.SelectionStart;
        Int32 selectionLength = textBox.SelectionLength;
        String newText = String.Empty;
        int count = 0;
        foreach (Char c in textBox.Text.ToCharArray())
        {
            if (Char.IsDigit(c) || Char.IsControl(c) || (c == '.' && count == 0))
            {
                newText += c;
                if (c == '.')
                    count += 1;
            }
        }
        textBox.Text = newText;
        textBox.SelectionStart = selectionStart <= textBox.Text.Length ? selectionStart : textBox.Text.Length;    
}

```
Here is how to create a numeric field in WPF, using the regular expressions.

XAML:

```
<TextBox PreviewTextInput="NumberValidationTextBox"></TextBox>

```

Code behind:

```
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}

```