Thursday, November 15, 2007

Number only TextBox

There are a lot of cases, you have to validate, that user inputs numbers only into TextBox. Someone things, that it's bad behavior to prevent user from typing well-known bad characters. I think, it's good. Let's take an example of form, you have to enter Social ID. Do you really think, that user expects the form tell him: "Hey, you, it's 9 digits only. You typed 'Z', which is not digit. Don't do it anymore...". I think, that the good behavior is when user types Z in text area for social id, nothing should happens. But, anyhow, it's up to you to decide.

Today, I want to explain how to build TextBox, that accepts only numbers in WPF.

In WinForms world, we used to handle KeyDown or KeyPressed event to get character and check whether it number. In WPF world, there are a lot of ways to input anything, that text (for example Voice recognition or handwriting. More then this, in WPF (device independent) world we can not be sure, that when you press key with 2 and @ in your keyboard without Shift pressed, you meant "2". Thus KeyDown or, even PreviewKeyDown event will not bring you salvation. We should handle input text without relation to input device. For this purpose, we have TextInput and PreviewTextInput event. Let's handle it

public class NumberTextBox:TextBox

        protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
            e.Handled = !AreAllValidNumericChars(e.Text);


Pretty cool, ah? What's about validation itself? "5" is definitely number, but what about "٥" ? Well, it's five in Arabic and it's number too. Is '½' is number? Sure, it's a half, but is it digit? No! This why we should use Char.IsDigit method, rather then Char.IsNumber which is not limited to radix 10 digits. The same behavior,   you'll get using Char.GetUnicodeCategory method to compare to UnicodeCategory.DecimalDigitNumber, but in this case, it will be current culture oriented.

Well. now we know if the input it decimal, what's about 1,000.00 numbers? The simplest way is to check whether the char is '.'{0x2e} or ',' {0x2c} and mark it as valid, however, what's about 1'00''00 or 100° which is absolutely valid numbers? NumberFormatInfo.CurrentInfo is your best friend in this case. You can always check my Globalization statements by using this handy program.

Now, when we sure, that everything is ok, we can write the test method

bool AreAllValidNumericChars(string str)
            bool ret = true;
            if (str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeSign |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeInfinitySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentSymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PerMilleSymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveInfinitySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveSign)
                return ret;

            int l = str.Length;
            for (int i = 0; i < l; i++)
                char ch = str[i];
                ret &= Char.IsDigit(ch);

            return ret;

The only thing we should do is handle drag and drop and paste whole text into the textbox. That's all folks. If you want to know how to handle and validate D&D and paste, let me know.


José said...

Hey man very nice code! Could you do the D&D and pasting validation?

praveen said...


Good work!! Can you please provide the pasting operation too..

Stas Lupinsky said...

Problem with this solution is that PreviewTextInput is not fired when spaces are entered.

Stas Lupinsky said...

Problem with this solution is that PreviewTextInput is not fired when spaces are entered.

Stas Lupinsky said...

Problem with this solution is that PreviewTextInput is not fired when spaces are entered.