Another control we are working on as part of the Editors for WPF product is the MaskedTextBox control. Built from scratch, this control allows user input to be restricted based on a regular expression pattern.
The regular expression supports simple literals and character classes, in addition to quantifiers (*, +, ?, etc) and alternations (a|b). For example, the following regular expression will match a U.S. phone number with or with a preceding area code or 1:
([(]\d\d\d[)] |(1-)?\d\d\d-)?\d\d\d-\d\d\d\d
Valid entries for this regular expression include:
- 555-1212
- (703) 555-1212
- 703-555-1212
- 1-703-555-1212
By default, the non-option regex elements will be presented using a configurable prompt character or glyph. Additionally, literal characters will be displayed when appropriate and are not required to be entered by the user.
|
A MaskedTextBox with the regex defined above, with no user input
|
When "5" is entered three times, the control fills in the three required regex elements and moves the caret over the dash, since it's a literal character and is required.
|
A MaskedTextBox with the regex defined above, with "555" typed as input
|
When "(70" is entered, then the left parenthesis indicates that the first entry in the regex alternation must be taken. Therefore, the proceeding "70" are entered in the area code. Additionally, two new literal characters are added, the ")" followed by a space, since they are required for that branch of the alternation.
|
A MaskedTextBox with the regex defined above, with "(70" typed as input
|
Finally, when "1-70" is entered, then the "1" would initially be associated with one of the required regex elements outside the alternation. But, when the "1" is followed by a "-" then it must take the second alternation branch, thus allowing "70" to be entered as the area code.
|
A MaskedTextBox with the regex defined above, with "1-70" typed as input
|
Prompt character/glyph
The prompt indicator can be any printable character, a custom Geometry (as seen above), or can be excluded all together. Additionally, the brush used for the prompt indicator can be defined separately from the foreground brush for the text. This allows the prompt indicator to have a subtle appearance, so it does not distract from the entered text.
Literal completion
By default, any literal characters will automatically be inserted when required. This saves the user from having to needlessly type common characters. It is also possible to require that the literal characters be entered manually. Therefore, the user must type every character in the regular expression into the MaskedTextBox.
Use with parts editor
The MaskedTextBox will be leveraged by the parts-based editors to help restrict and validate user input. So that things like the year part of DateTime parts-editor, only allows digits. Additional hooks will be available to verify that the "digits" actually equate to a valid year. This hooks will be available for general use, so that restricting and validating input will be a snap.