user995689
user995689

Reputation: 825

Silverlight textbox to accept only decimals

I have a Silverlight app with a textbox whose input I want to limit to decimal numbers only. Searching the web I came across the following possible solution (curiously in different places with different people claiming authorship of the same lines of code) It appears to work well except that after at least 1 numeral has been entered it will then allow the letter 'd' in either upper or lower case to be entered, I can't figure out why that is and thus can't figure out how to prevent that from happening. Could anyone please provide a solution. Many thanks.

    private void Unit_KeyDown(object sender, KeyEventArgs e)
    {

        if (e.Key == Key.Tab)
        {

        }
        var thisKeyStr = "";
        if (e.PlatformKeyCode == 190 || e.PlatformKeyCode == 110)
        {
            thisKeyStr = ".";
        }
        else
        {
            thisKeyStr = e.Key.ToString().Replace("D", "").Replace("NumPad", "");
        }
        var s = (sender as TextBox).Text + thisKeyStr;
        var rStr = "^[0-9]+[.]?[0-9]*$";
        var r = new Regex(rStr, RegexOptions.IgnoreCase);
        e.Handled = !r.IsMatch(s);

    }

Upvotes: 0

Views: 3508

Answers (3)

Muhammad Bilal
Muhammad Bilal

Reputation: 389

      private void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
        bool isDigit = e.Key >= Key.D0 && e.Key < Key.D9 || e.Key == Key.NumPad0 || e.Key == Key.NumPad1 || e.Key == Key.NumPad2 || e.Key == Key.NumPad3 || e.Key == Key.NumPad4 || e.Key == Key.NumPad5 || e.Key == Key.NumPad6 ||
        e.Key == Key.NumPad7 || e.Key == Key.NumPad8 || e.Key == Key.NumPad9 ||e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Left || e.Key == Key.Right;

        if (isDigit) { }
        else
            e.Handled = true; 
    }

Upvotes: 0

CodeNepal
CodeNepal

Reputation: 902

here is a easier code optimized. NO object creation; NO string comparision and NO regex validation

private static void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
        //platform code for Hyphen which is not same as Subtract symbol but in our case both give same meaning
        const int KEYCODE_Hyphen_OnKeyboard = 189;
        const int KEYCODE_Dot_OnKeyboard = 190;
        const int KEYCODE_Dot_OnNumericKeyPad = 110;

        e.Handled = !(
            (!( //No modifier key must be pressed
                 (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift
              || (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control
              || (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt
            )
          && ( //only these keys are supported
                (e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
              || e.Key == Key.Subtract || e.Key == Key.Add || e.Key == Key.Decimal
              || e.Key == Key.Home || e.Key == Key.End || e.Key == Key.Delete
              || e.Key == Key.Tab || e.Key == Key.Enter || e.Key == Key.Escape || e.Key == Key.Back
              || (e.Key == Key.Unknown && (
                         e.PlatformKeyCode == KEYCODE_Hyphen_OnKeyboard
                      || e.PlatformKeyCode == KEYCODE_Dot_OnKeyboard || e.PlatformKeyCode == KEYCODE_Dot_OnNumericKeyPad
                    )
                 )
             )
          )
        );
    }

Upvotes: 2

Nuffin
Nuffin

Reputation: 3972

You could try the following:

  1. Replace the else with else if (e.Key != Key.D) or
  2. set the Handled property like this:

    e.Handled = !r.IsMatch(s) || string.IsNullOrEmpty(thisKeyStr);
    
    // also possible:
    e.Handled = !r.IsMatch(s) || e.Key == Key.D;
    

Upvotes: 2

Related Questions