Skip to content
Morten Nielsen edited this page Jun 10, 2015 · 3 revisions

This trigger allows you to validate against a regular expression. Properties:

  • Value : The string value you want to evaluate against an expression (required)
  • Expression : The regular expression used for match evaluation (required)
  • Options : Regular expression option enum flag (optional - defaults to 'None')

###Example Evaluate an email in the textbox 'emailTextBox':

<triggers:RegexStateTrigger Value="{x:Bind emailTextBox.Text, Mode=OneWay}"
		Expression="^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"
		Options="IgnoreCase"
		/>

regextrigger

Full sample for the above screen recording:

<Grid>

	<VisualStateManager.VisualStateGroups>
		<VisualStateGroup >
			<VisualState x:Name="validEmail">
				<VisualState.StateTriggers>
					<triggers:RegexStateTrigger 
						Value="{x:Bind emailTextBox.Text, Mode=OneWay}"
						Expression="^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"
						Options="IgnoreCase" />
				</VisualState.StateTriggers>
				<VisualState.Setters>
					<Setter Target="emailStatus.Text" Value="This is a valid email" />
					<Setter Target="emailStatus.Foreground" Value="Green" />
					<Setter Target="submitButton.IsEnabled" Value="true" />
				</VisualState.Setters>
			</VisualState>
		</VisualStateGroup>
	</VisualStateManager.VisualStateGroups>

	<StackPanel VerticalAlignment="Center">
		<TextBox x:Name="emailTextBox" Header="Enter an email" />
		<TextBlock x:Name="emailStatus" Foreground="Red" Text="Not a valid email" />
		<Button x:Name="submitButton" Content="Submit" IsEnabled="False" />
	</StackPanel>

</Grid>
Clone this wiki locally