The SftMask.Mask property defines the edit mask: the input positions where the user types, the numeric fields, and the literal characters the field shows between them. A mask consists of tokens, which define input positions and fields, and literal characters. An empty mask makes the control a plain single-line edit field (see Simple Edit Field).
Setting a new mask clears the contents. An invalid mask throws an ArgumentException whose message describes the error, such as the position in the mask where it is invalid, and the control keeps its previous mask. A mask can be up to 1,023 characters long and define up to 1,023 positions. The property pages offer predefined masks and a field to try a mask (see Control Properties); typical masks are listed in Common Masks.
The masks on this page are shown as they are typed in the Properties window. In C# code, use a verbatim string (@"...") or write each backslash twice.
| Token | Description |
|---|---|
| # | Digit, required. |
| 9 | Digit, optional. |
| ? | Letter, required. |
| @ | Letter, optional. |
| A | Letter or digit, required. |
| a | Letter or digit, optional. |
| & | Any character, required. |
| ~ | Any character, optional. |
| [Mchars] | One of the listed characters, required. |
| [Ochars] | One of the listed characters, optional. |
A list holds single characters and ranges such as A-Z or 1-4. A minus sign at the start of the list or right after a range is a character of its own, so [M-+] accepts a minus or a plus sign; a range without an end at the end of the list, as in [M0-], accepts its first character and every character with a higher code. A backslash makes the next character part of the list, so [M\]] accepts a closing bracket and [M\\] a backslash.
| Example | |
|---|---|
| [Mabc] | a, b or c |
| [MA-Z1-4] | An uppercase letter, or a digit 1 to 4 |
| [M1-9]## | A three-digit number that does not start with 0 |
| [OYN] | Y, N or nothing |
Optional positions can stay empty. A typed character that does not fit an empty optional position skips it and is entered at the following position. The SftMask.AsciiOnly property limits all input positions to ASCII characters.
Every character that is not a token is a literal: the field shows it, and the user types past it. A backslash makes the next character a literal, also when it is a token: \# is the character #, \9 the digit 9, \$ a dollar sign, \\ a backslash. A dollar sign, an opening bracket, <, ! and = start tokens, so they need the backslash to be literals; characters that are not tokens, such as parentheses, dashes and spaces, can be written with or without a backslash.
The caret moves past literals by itself, so the user types only the input, such as the ten digits of a phone number. When the caret is in front of a literal, typing that character moves past it, and a literal the caret has just moved past can be typed as well, so (555) 555-0123 can be typed with or without its parentheses, space and dash (SftMask.SkipLiterals property). Typed at an empty optional position, the character of the next literal leaves the optional positions before it empty, so typing 12-34 into 999\-9999 enters 12 -34.
These tokens are replaced by the conventions of the control's culture (SftMask.Culture property, see Cultures):
| Token | Description |
|---|---|
| | | The currency symbol. |
| : | The time separator. |
| / | The date separator. |
A numeric field holds a number. It starts with a dollar sign, followed by subtokens that define the number, in the order of the tables below; $, $I and $X need at least one of them, as in $I4. The number is shown right aligned in the field, formatted with the culture's decimal and group separators, and numbers are typed as on a calculator (see Numeric Fields). A mask can hold several numeric fields, such as the four parts of an IP address. Masks with a numeric field do not use autocomplete.
| Subtoken | Description | |
|---|---|---|
| ^ | (optional) | The field can be spun with the up and down buttons and the Up and Down keys (see Up/Down Buttons and Spinning). |
| C | (optional) | Typing an operator opens the popup calculator (see Popup Calculator). |
| N | (optional) | Spinning stops at the ends of the range instead of wrapping around. |
| ( or - | (optional) | Negative numbers are allowed, shown in parentheses or with a minus sign. |
| , | (optional) | Digit grouping, with the culture's group separator and group sizes. |
| 0 | (optional) | Leading zeros fill the field. Cannot be combined with digit grouping or fraction digits. |
| digits | The number of digits before the decimal separator (1 if left out). | |
| .fraction | (optional) | The number of fraction digits (1 if only the period is given). |
| (min,max) | (optional) | The smallest and largest value. Both can have fraction digits and a minus sign. |
| Subtoken | Description | |
|---|---|---|
| ^ | (optional) | The field can be spun. |
| N | (optional) | Spinning stops at the ends of the range instead of wrapping around. |
| - | (optional) | Negative numbers are allowed. |
| , | (optional) | Digit grouping. |
| 0 | (optional) | Leading zeros fill the field. Cannot be combined with digit grouping. |
| digits | The number of digits (1 if left out). | |
| (min,max) | (optional) | The smallest and largest value. |
| Subtoken | Description | |
|---|---|---|
| ^ | (optional) | The field can be spun. |
| N | (optional) | Spinning stops at the ends of the range instead of wrapping around. |
| 0 | (optional) | Leading zeros fill the field. |
| digits | The number of hexadecimal digits, up to 23 (1 if left out). | |
| (min,max) | (optional) | The smallest and largest value, in hexadecimal. |
A decimal or integer field holds up to 28 digits in all, and each digit count has one or two digits. A range needs both values, and it is used when they differ; without a range, the number of digits defines the largest value. Negative numbers need the ( or - subtoken, also in a field whose range starts below zero.
| Example | |
|---|---|
| $I4 | An integer of up to 4 digits. |
| $I^2(10,49) | An integer from 10 to 49 with up and down buttons. |
| $I^03(0,255) | An integer from 0 to 255 shown with leading zeros, with up and down buttons. |
| $-,10.3 | A decimal number with up to 10 digits and 3 fraction digits, negative numbers and digit grouping. |
| $^C-,8.2 | An amount with up to 8 digits and 2 fraction digits, negative numbers, digit grouping, up and down buttons and the popup calculator. |
| $C(,8.2(-1000,10000) | An amount from -1,000 to 10,000 with the popup calculator and digit grouping, negative numbers shown in parentheses. |
| $^3(0,100) | A percentage from 0 to 100 with up and down buttons. |
| $X^02(0,FF) | A hexadecimal byte with leading zeros and up and down buttons. |
Dates and times are not part of the mask syntax; they are entered with SftDate/NET.
| Token | Description |
|---|---|
| = | A shift break: inserting and deleting characters does not shift characters past this point. |
| <u ... > | The input is converted to uppercase. |
| <l ... > | The input is converted to lowercase. |
| <p ... > | The input is shown as the password character defined by the SftMask.PasswordChar or SftMask.UseSystemPasswordChar property; without one, it is shown as typed. |
| !n ... !! | A value token: the positions from !n (a digit 0 to 9) to !! (or to the end of the mask) form a value the application reads and sets through the SftMask.Values property (see Text and Values). Each digit can be used once, and a value token ends with !! before the next one starts. |
A > ends every open <u, <l and <p block; a block without > extends to the end of the mask, and a > outside a block is a literal. The blocks can be combined, as in <l<p????#>.
| Example | |
|---|---|
| ####=####=####=#### | A credit card number in four groups; deleting a digit in one group does not shift the other groups. |
| <uAAA\-AAAA> | A license plate in uppercase. |
| <p####> | A PIN of four digits, shown as password characters when the SftMask.PasswordChar or SftMask.UseSystemPasswordChar property is set. |
| !1####!!\-!2##!! | Two value tokens; SftMask.Values[1] holds the first four digits and SftMask.Values[2] the last two. |