inputscope

How to Limit the Input of a Textbox of Windows Phone

Some time ago I have written about the keycode glitch. Other than this glitch, sometimes you might need to limit the scope of input to certain set of characters.Changing the inputscope of the keyboard helps alot. But, sometimes you need more specific filters. For example, if you’re expecting an all-numbers input and you use the “TelephoneNumber” keyboard, you’ll still be able to get “.” or other symbols. Another example is if you need to get an email address input which you would prefer to have in small letters.

Anyway, let’s get to the code. The code is simply one line. Lets assume that you have textbox name txtInput. You’ll just have to add the following code in the event handler of “KeyDown” for the txtInput control.

[crayon lang=”vb”]If “0123456789”.IndexOf(e.Key.ToString) = -1 Then e.Handled=True[/crayon]

This line limits the input of the textbox to the numbers 0 to 9. You can change the content between the qoutations into any range of acceptable input characters that you need.

I hope this helps.