Mask Options in WPF MaskedTextBox (SfMaskedEdit)
Each MaskType
has different set of mask elements that are combined to form a mask expression. Based on the complexity and usage, mask types are classified as:
- Simple
- Regular
- RegEx
Simple
Expressions generated with very simple mask elements comes under this group. This is mainly used for fixed length inputs. For example, phone number.
Mask elements in mask type are as follows.
Simple mask elements
Elements | Description |
---|---|
0 | Digit and required. These elements accepts any single digit between 0 and 9. |
9 | Digit or space and optional. |
# | Digit or space and optional. Plus (+) and minus (-) signs are allowed. |
L | Letter and required. Restricts input to the ASCII letters a-z and A-Z. These mask elements are equivalent to [a-z A-Z] in regular expressions. |
? | Letter and optional. Restricts input to the ASCII letters a-z and A-Z. These mask elements are equivalent to [a-z A-Z]? in regular expressions. |
C | Character and optional. |
A | Alphanumeric and required. |
< | Shift down. Converts all the characters to lowercase. |
> | Shift up. Converts all the characters to uppercase. |
<syncfusion:SfMaskedEdit MaskType="Simple" Mask="00/00/0000" Width="255" Height="46"/>
SfMaskedEdit maskededit = new SfMaskedEdit();
maskededit.MaskType=MaskType.Simple;
maskedEdit.Mask="00/00/0000";
Regular
Regular mask supports simple regular expression with following elements.
Regular mask elements
Elements | Description |
---|---|
\w | Accepts any alphabet. |
\d | Accepts any digit. |
[0-9A-Z] | Accepts any digit between 0-9 and character between A-Z. |
{n} | Accepts input for n number of times. |
{n,m} | Accepts input for n minimum number of times and m maximum number of times. |
? | Optional input. |
+ | Accepts input for one or more times. |
* | Accepts input for zero or more times. |
<syncfusion:SfMaskedEdit MaskType="Regular" Mask="(\d{0,3})\d{3}-\d{2}-\d{2}" Width="255" Height="46"/>
SfMaskedEdit maskededit = new SfMaskedEdit();
maskededit.MaskType = MaskType.Regular;
maskedEdit.Mask = "(\d{0,3})\d{3}-\d{2}-\d{2}";
RegEx
Extended regular expression with more complex expression which gives more usage of input data.
Provided optional, positive, and negative lookahead support in the SfMaskedEdit control.
Optional
“*” is a special character that denotes zero or more matches for the preceding character.
For example, for input “\d\w\d” – The output can be “12” or “1a1” or “123”, because the “\w” is optional. If the user type the input “1a”, the character “a” will match with optional mask and the masked text will be “1a_”. If the mask text is “12” and if the character “3” is typed, the character “2” will be considered as optional mask and the masked text will be “123”.
Positive Lookahead Regex
Positive lookahead Regex matches a group after the main expression without including it in the result.
“(?=regex)” is the Positive lookahead regex. It matches only when the regex is matched with input text.
For example, for input “(?=123)\d{3}”: It allows first two digit. While entering the third digit, it will validate the regex and restrict the input. It allows the output only when the MaskText is “123” for this given input.
Negative Lookahead Regex
Negative lookahead Regex specifies a group that cannot match after the main expression. If it matches the input text, the result will be discarded.
“(?!regex)” is the Negative lookahead regex. It validates the input on every keypress.
For example, for input “(?!55)(?!000)(?!666)\d{3}” – \d{3} denotes that the output will be of 3 character length. “(?55)” denotes the value will not begin with “55”. “(?!000)(?!666)” denotes the output cannot be “000” or “666”.
RegEx mask elements
The RegEx mask elements are as follows.
Elements | Description |
---|---|
abc|def | Accepts any one input that matches. |
(a|b)+ | Accepts the matching input in one or more occurrences. |
[ABC] | Accepts any single character included in the specified set of characters. |
[^ABC] | Accepts any single character not included in the specified set of characters. |
[A-Z] | Accepts any character between two specified set of characters inclusive. |
(ABC) | Matches a group specified within the parenthesis. |
\d | Accepts any digit. Same as [0-9]. |
\D | Accepts any non-digit. Same as [^0-9]. |
\w | Accepts any word character. \w is the same as [a-zA-Z_0-9]. |
\W | Accepts any non-word character. \W is the same as [^a-zA-Z_0-9]. |
\s | Accepts any white space characters. |
\S | Accepts any non-white space characters. |
(?=ABC) | Matches a group after the main expression without including it in the result. |
(?!ABC) | Specifies a group that cannot match after the main expression. |
{n} | Accepts the input for n number of times. |
{n,} | Accepts the input for 'n' and more than 'n' number of times. |
{n,m} | Accepts the input for n minimum number of times and m maximum number of times. |
+ | Accepts one or more matches for the preceding character. |
* | Accepts zero or more matches for the preceding character. |
? | Optional input (Zero or one occurrence of the matching input). |
| | Acts like a Boolean OR. Matches the expression before or after the |. |
. | Accepts any character. It can be changed based on culture. |
For example, the following code example shows the validation for E-mail using Regex elements.
<syncfusion:SfMaskedEdit MaskType="RegEx" Mask="[A-Za-z0-9._%-]+@[A-Za-z0-9]+.[A-Za-z]{2,3}" Width="255" Height="46"/>
SfMaskedEdit maskededit = new SfMaskedEdit();
maskededit.MaskType = MaskType.RegEx;
maskedEdit.Mask = "[A-Za-z0-9._%-]+@[A-Za-z0-9]+.[A-Za-z]{2,3}";