# Regex Validation

> important This feature is supported only by the **RadMaskedTextInput** control.

RadMaskedTextInput allows you to enable an additional validation rule by defining a regular experession. The regex is applied over the value received after the Mask property validates the entered text.

## Setting Regular Expression

The regex can be set via the **ValidationRegex** property of the RadMaskedTextInput control.

**Example 1: Setting regex validation**

```XAML
<telerik:RadMaskedTextInput Mask="a20"                                    
								ValidationRegex="^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$" 
								maskedInput:ValidationHelper.DisplayValidationErrors="True"  />
```

**Example 1** shows how to restrict the user to enter only a valid email address with a maximum text length of 20 alphanumeric or special characters.

> Take the [ValueMode](https://www.telerik.com/products/wpf/documentation/controls/radmaskedinput/features/value-mode) property into account when composing the regular expression. Depending on the mode the Value of the control might contain placeholders and literals.

## Predefined Regular Expressions

The validation feature provides few predefined regular expressions that can be used.

* **Email**: This expression allows you to validate email addresses.
* **IP**: This expression allows you to validate IP addresses.

You can access the predefined regular expressions via the **ValidationHelper** class. It exposes the regexes via static readonly properties.

> The ValidationHelper class can be found in the following namespace: `xmlns:maskedInput="clr-namespace:Telerik.Windows.Controls.MaskedInput;assembly=Telerik.Windows.Controls.Input"`

**Example 2: Setting predefined regex in XAML**

```XAML
<telerik:RadMaskedTextInput ValidationRegex="{x:Static maskedInput:ValidationHelper.Email}" />
```

**Example 3: Setting predefined regex in code**

```C#
this.maskedTextInput.ValidationRegex = ValidationHelper.Email;
```

## Set Error Message

You can set the error that will be shown in the validation tooltip via the **ErrorMessage** property of the RadMaskedInput control.

**Example 4: Setting predefined error message in XAML**

```XAML
<telerik:RadMaskedTextInput ErrorMessage="Invalid email address"/>
```

> caption Custom error message

![WPF RadMaskedInput Custom error message](https://www.telerik.com/products/wpf/documentation/assets/d9c427dd6cd1f0b74c1251c7fc7a85f4/radmaskedinput-features-regex-validation-0.png)

## Display Validation Errors

The visual element that presents the validation errors is not displayed by default when there is an error. If you want to display it set the **ValidationHelper.DisplayValidationErrors** attached property on the masked input control to **True**.

**Example 5: Enabling the validation errors visual**

```XAML
<telerik:RadMaskedTextInput maskedInput:ValidationHelper.DisplayValidationErrors="True"  />
```

## See Also

* [Getting Started](https://www.telerik.com/products/wpf/documentation/controls/radmaskedinput/getting-started)
* [Mask Tokens](https://www.telerik.com/products/wpf/documentation/controls/radmaskedinput/features/mask-tokens)
