How can I restrict TextBox to accept only capital letters, or for example digits, or forbid to put any special character?
Sure it's a piece of cake to catch TextInput event and handle the text here, but is it the proper way to do this?
How can I restrict TextBox to accept only capital letters, or for example digits, or forbid to put any special character?
Sure it's a piece of cake to catch TextInput event and handle the text here, but is it the proper way to do this?
I've improved Kent Boogaart's answer by also handling the following actions that previously could cause the pattern to be violated:
For example, Kent Boogaart's answer allowed the user to enter "ac" by first entering "abc" and afterwards delete the "b" with the backspace which violates the following regex
Usage (unchanged):
<TextBox b:Masking.Mask="^\p{Lu}*$"/>
Mask class:
I changed VitalyB's code to support Color Themes. Instead of blocking user input if it doesn't meet the RegEx script, it just highlights the text box. The text box will be the theme default without interaction, and then defaults to a light green or red depending on the value after the input is set. You can also set the fail and pass colors programatically with:
The class is below:
To run, the script requires a class written by Aaron C, explained here: Silverlight/WPF sets ellipse with hexadecimal colour shown here: http://www.wiredprairie.us/blog/index.php/archives/659
The code is below in case the website is ever moved:
I've done this in the past with an attached behavior, which can be used like this:
The attached behavior code looks like this:
Another possible solution is to use one of the "Masked TextBox" wpf implementation using the "MaskedTextProvider" class that is used by the "MaskedTextBox" from Winforms.
Two potential solutions are found here -> https://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox and here -> http://marlongrech.wordpress.com/2007/10/28/masked-textbox/