The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
[UWP] How to enter only numbers/digits in textbox RRS feed

  • Question

  • Hi,

    I am debeloping an App. Is there any textbox property or way so that user can enter only number in the textbox?

    How can I achieve this?

    Thanks. 

    Tuesday, October 13, 2015 10:17 AM

Answers

  • Key events won't catch everything here: not all text input comes through key events. Non-key input can come from ink, speech, IMEs, etc.

    Instead track the text changes. Since you're writing a Universal app (rather than a Windows 8.1 app) the TextBox has a TextChanging event you can track to know when a text change is proposed (Windows 8.1 has only TextChanged, so you'd have to do this after the fact). The TextChanged handler can check the Text to see if it is valid and then reset the Text to remove any non-numeric characters.

    For more complex input you'll want to reject things later so you don't prevent an invalid temporary state that will become valid if the user continues typing.

    • Proposed as answer by NekoLars Wednesday, October 14, 2015 7:17 AM
    • Marked as answer by TejD Wednesday, October 14, 2015 8:33 AM
    Tuesday, October 13, 2015 8:37 PM
  • After some searching there is no way to simply block the input of letters. Best way is indeed how Rob explains to catch the input and then delete if it is not a numeric input.
    • Marked as answer by TejD Wednesday, October 14, 2015 8:33 AM
    Wednesday, October 14, 2015 7:18 AM

All replies

  • Hi TejD

    You can achieve this be adding this to the textbox xaml: InputScope="Number". This will also change the keyboard layout on mobile phones to only a number keyboard. 


    • Edited by NekoLars Tuesday, October 13, 2015 12:36 PM
    Tuesday, October 13, 2015 12:36 PM
  • Hi NekoLars,

    I tried the above, also in mobile number keypad comes. I also tried digit.

    But there is an issue, eventhough we have added this property.Below things I have observed :

    On PC/Desktop

    - I am able to write alphabets too

    On Mobile Emulator

    - May not be issue as number keypad comes  in mobile but I am able to write alphabets through normal keyboard.

    What is wrong?

    Thanks.

    Tuesday, October 13, 2015 1:01 PM
  • On desktop you have to intercept some events to make it work. There is no "dedicated" number keyboard on desktop. This is due to support of hardware keyboards, which can't just "hide" their alphabet keys. The virtual keyboard is basically a software that is interfaced to the keyboard standard input and as such it must always provide hooks to all standard keys.
    Tuesday, October 13, 2015 1:16 PM
  • Hi,

    What are the events should I consider to make it work on Desktop?

    Thanks.

    Tuesday, October 13, 2015 1:27 PM
  • Hi,

    On the Desktop mode, you can listen to the KeyUp or KeyDown event of the textbox. Once the Enter key or Search key, whatever, is hit, the KeyUp or KeyDown event will be fired with an argument indicates what virtual key just hit.

    In your case, just determine the e.Key == VirualKey.Enter and do some error prompt.

    Good luck.


    Please mark my reply as Answer if it helps.

    Tuesday, October 13, 2015 2:54 PM
  • Key events won't catch everything here: not all text input comes through key events. Non-key input can come from ink, speech, IMEs, etc.

    Instead track the text changes. Since you're writing a Universal app (rather than a Windows 8.1 app) the TextBox has a TextChanging event you can track to know when a text change is proposed (Windows 8.1 has only TextChanged, so you'd have to do this after the fact). The TextChanged handler can check the Text to see if it is valid and then reset the Text to remove any non-numeric characters.

    For more complex input you'll want to reject things later so you don't prevent an invalid temporary state that will become valid if the user continues typing.

    • Proposed as answer by NekoLars Wednesday, October 14, 2015 7:17 AM
    • Marked as answer by TejD Wednesday, October 14, 2015 8:33 AM
    Tuesday, October 13, 2015 8:37 PM
  • Hi,

    This means, we need to enter the text and then validate it and then remove it(non numeric) from textbox.

    TextChange Event is more appropriate.

    how we will remove the invalid text from textbox after validating it? I didnot get that

    Like in asp it donot allow non-numeric character to enter in textboxbox itself, Is it possible to achieve same in UWP?

    or we have to do as stated above.

    Thanks

    Wednesday, October 14, 2015 6:27 AM
  • After some searching there is no way to simply block the input of letters. Best way is indeed how Rob explains to catch the input and then delete if it is not a numeric input.
    • Marked as answer by TejD Wednesday, October 14, 2015 8:33 AM
    Wednesday, October 14, 2015 7:18 AM
  • public void KeyDown(object sender, KeyRoutedEventArgs e) {

    if (e.Key.ToString().Equals("Back"))
                    {
                        e.Handled = false;
                        return;
                    } for (int i = 0; i < 10; i++) { if (e.Key.ToString() == string.Format("Number{0}", i)) { e.Handled = false; return; } } e.Handled = true; }




    Sunday, April 17, 2016 10:58 AM
  • That will only adjust key input and will allow non numbers by other means.. See my previous response.
    Sunday, April 17, 2016 4:09 PM
  • Sorry for late answer, just follow steps on this article.

    I have created simple class that can be passed in Tag property of TextBox and using TextChanging Event you can further process to allow numeric input only, restrict if accepts only positive values, and format text according to numeric custom pattern of your choice according to culture set on your application.

    Many Regards

    Monday, November 21, 2016 7:03 AM