locked
[UWP][Xaml]Need my textbox should take only some charecters RRS feed

  • Question

  • Hi

      I am in the development of windows universal application, Here I am having one "TextBox" to enter the Mobile numbers,

      I want my textbox to allow only to enter the below characters.(In Keydown event)

      1.  0-9 Numbers

      2.  (

      3.  )

      4.  -

      5.  BackSpace

       We have tried a lot but we could not make it success.

       please provide us the code to achieve the above task.

      -Thanks & Regards

    Sunil


    Thursday, February 25, 2016 11:03 AM

Answers

  • You can handle the TextChanging event to edit out invalid characters. You can't readily catch all input paths before they get to your TextBox, but you can remove them before they are shown. Paulio's link is for WinForms, not UWP apps (and Is incorrect for WinForms as well: not all text input comes through key events)
    • Marked as answer by Barry Wang Monday, March 7, 2016 3:19 AM
    Thursday, February 25, 2016 3:55 PM
  • Modern text input is much larger than just KeyUp and KeyDown. You really need to do this in TextChanged rather than in KeyUp and KeyDown. Filtering in key events will miss non-key input such as IMEs, ink, speech, keyboard predictions, paste, etc.
    • Proposed as answer by Barry Wang Monday, February 29, 2016 5:42 AM
    • Marked as answer by Barry Wang Monday, March 7, 2016 3:19 AM
    Thursday, February 25, 2016 9:58 PM

All replies

  • like this? https://social.msdn.microsoft.com/Forums/en-US/24c3bf8c-004d-443f-82b5-8af9f23e0358/how-can-i-limit-my-textbox-text-length-takes-9-digits-only-using-regex?forum=winformsapplications


    http://pauliom.wordpress.com

    Thursday, February 25, 2016 2:32 PM
  • You can handle the TextChanging event to edit out invalid characters. You can't readily catch all input paths before they get to your TextBox, but you can remove them before they are shown. Paulio's link is for WinForms, not UWP apps (and Is incorrect for WinForms as well: not all text input comes through key events)
    • Marked as answer by Barry Wang Monday, March 7, 2016 3:19 AM
    Thursday, February 25, 2016 3:55 PM
  • Yes it's winforms but the regex for filtering out the keys is the way I'd go.

    http://pauliom.wordpress.com

    Thursday, February 25, 2016 9:38 PM
  • Modern text input is much larger than just KeyUp and KeyDown. You really need to do this in TextChanged rather than in KeyUp and KeyDown. Filtering in key events will miss non-key input such as IMEs, ink, speech, keyboard predictions, paste, etc.
    • Proposed as answer by Barry Wang Monday, February 29, 2016 5:42 AM
    • Marked as answer by Barry Wang Monday, March 7, 2016 3:19 AM
    Thursday, February 25, 2016 9:58 PM
  • Can you please suggest us with the proper code please..
    Friday, February 26, 2016 6:30 AM
  • Try it yourself, then if you have a problem you can post your code and we can help

    http://pauliom.wordpress.com

    Friday, February 26, 2016 8:01 AM
  • @Sunil A M,

    Please follow Rob's idea to try something first, I think on MSDN you can already see some sample code TextChanging event.

    Best regards,

    Barry


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 29, 2016 5:44 AM