New to Telerik UI for Xamarin? Download free 30-day trial

Validation through custom Regular Expressions

One of the main features that RadMaskedInput supports is validation through a custom regular expression. You can define a input pattern of your choice and set it to the Mask property of the control. In order to use the validation through regular expressions, you should set the MaskType to Regex.

Below you can find a list of the available Regex and their usage:

Corresponding Regex Usage
"[0-9]" a single digit
"[0-9]{1}" a single digit(required)
"[a-zA-Z]" a single letter
"[a-zA-Z]{1}" a single letter(required)
"\S" all symbols without space
"." all symbols
"[0-9a-zA-Z]" all without special symbols and space
"[0-9a-zA-Z ]" all without special symbols

Custom Regex Example

MaskedInput with custom RegEx validation. The Keyboard type is Numeric.

<telerikInput:RadMaskedInput MaskType="Regex"
                             InvalidInputErrorText="Invalid date format!"
                             ErrorColor="Red"
                             WatermarkText="Enter Date"
                             Keyboard="Numeric"
                             Mask="^[0-9]{4}\/(30|31|[0-2]{0,1}[0-9]{1})\/(10|11|12|[0-9]{1})$"/>

Mask Extensions

The static class Telerik.XamarinForms.Input.MaskedInput.MaskExtensions contains two static regular expressions which you can directly use for validation when the MaskType is Regex:

  • IP: "^(([1-9]?\d|1\d\d|2[0-5][0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|2[0-5][0-5]|2[0-4]\d)$"
  • Email: "^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$"

Built-in Regex Example

Validation using the MaskExtensions class. The MaskedExtention type is Email. Note that the Keyboard type is set to Email.

<telerikInput:RadMaskedInput MaskType="Regex"
                             InvalidInputErrorText="Invalid E-Mail format!"
                             ErrorColor="Red"
                             Keyboard="Email"
                             WatermarkText="Enter E-mail"
                             Mask="{x:Static extensions:MaskExtensions.Email}"/>

Where the following namespace is used:

xmlns:extensions="clr-namespace:Telerik.XamarinForms.Input.MaskedInput;assembly=Telerik.XamarinForms.Input"

See Also

In this article