MaskType in Xamarin Masked Entry (SfMaskedEdit)

8 Jun 20213 minutes to read

Each MaskType has different set of mask characters that are combined to form a mask expression. Based on the complexity and usage, mask types are classified as:

  • Text
  • RegEx

Text

The expressions that are generated with letters, digits, and special characters come under this group. This is mainly used for fixed length inputs. For example: phone number, zip code, and so on.

Text Mask characters

Characters

Description

A Alphanumeric, required.
a Alphanumeric, optional.
L Letter, required. Restricts input to the ASCII letters a-z and A-Z.
l Letter, optional. Restricts input to the ASCII letters a-z and A-Z.
0 Digit, required. This character accepts any single digit between 0 and 9
9 Digit or space, optional.
# Digit or space, optional. Plus (+) and minus (-) signs are allowed.
C Character, optional.
\ Escapes a mask character, turning it into a literal. "\" is the escape sequence for a backslash.
Any other characters Considered as literals. Literals always occupy a static position in the mask at run time, and cannot be moved or deleted.
<syncmaskededit:SfMaskedEdit x:Name="maskedEdit" MaskType="Text" Mask="+1(000)000000"/>
SfMaskedEdit maskedEdit = new SfMaskedEdit();
maskedEdit.MaskType = MaskType.Text;
maskedEdit.Mask = "+1(000)000000";

This mask expression allows only numeric inputs in the places of 0.

Regex

The expressions that are generated with regular expressions come under this group, preferable for variable length inputs and input in range. For example: hexadecimal values [0-9A-C].
The regular expressions provide significant advantages when creating masks as compared with other mask modes.

Advantages

  • Allows you to enter the value of indeterminate length.
  • Restricts with specific pattern. Example email, password, and more.
  • Restricts you to enter specific range at specific position.

Regex Mask Characters

Characters

Description

\w Accepts any alphabet, number, including the _(Underscore) character.
\d Accepts any digit.
{n} Accepts the input for ‘n’ number of times.
{n,m} Accepts the input for ‘n’ minimum number of times and ‘m’ maximum number of times.
? Optional input.
+ Accepts the input for one or more times.
* Accepts the input for zero or more times.
[aeiou] Accepts any single character included in the specified set of characters.
[0-9a-fA-F] Accepts any character between[A-F]/[a-f] and numbers between [0-9].

NOTE

SfMaskedEdit control only supports the above listed regex mask characters.

<syncmaskededit:SfMaskedEdit x:Name="maskedEdit" MaskType="RegEx" Mask="+1\(\d{3}\)\d{5}"/>
SfMaskedEdit maskedEdit = new SfMaskedEdit();
maskedEdit.MaskType = MaskType.RegEx;
maskedEdit.Mask = @"+1\(\d{3}\)\d{5}";

This mask expression ’\d{3}’ and’ \d{5}’ allows only numeric, where {n} is the count that the input should be accepted.

MaskType support in Xamarin.Forms masked edit

Troubleshooting

In case of having the requirement is to type the special characters such as _ and @, you can specify that directly in Mask as per your required order.

If you use the mask with “_” then typed character _ (underscore) will not be accepted since the default value of the PromptChar is _ (underscore). Hence, it will be replaced by the character when you enter. To avoid this, you can set the PromptChar text other than _ (underscore) as shown below,

<syncmaskededit:SfMaskedEdit x:Name="maskedEdit" MaskType="RegEx" Mask="[a-z0-9A-Z@_#*()-/$]*" PromptChar="*"/>
SfMaskedEdit maskedEdit = new SfMaskedEdit();
maskedEdit.MaskType = MaskType.RegEx;
maskedEdit.Mask = @"[a-z0-9A-Z@_#*()-/$]*";
maskedEdit.PromptChar = '*';