How to use PatternErrorMessage

Jan 8, 2014 at 7:10 PM
Edited Jan 8, 2014 at 7:56 PM
I am wondering if anyone could offer a tip about how to use PatternErrorMesage.

I have the following code which works.
<TextBox x:Name="tbEmail" Width="400" Margin="10 0"
    WinRTXAMLToolkitExtensions:FieldValidationExtensions.Format="MatchesRegexPattern"
    WinRTXAMLToolkitExtensions:FieldValidationExtensions.Pattern="^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$"
    WinRTXAMLToolkitExtensions:FieldValidationExtensions.PatternErrorMessage="Invalid email address."/>
However I do not know what is the best way to utilize PatternErrorMessage. Can it be linked to TextBlock to display it?

Hong
Coordinator
Jan 8, 2014 at 8:50 PM
Edited Jan 8, 2014 at 8:51 PM
Yes, if you look at the sample here - https://winrtxamltoolkit.codeplex.com/SourceControl/latest#WinRTXamlToolkit.Sample/Views/Controls.Extensions/TextBoxValidationTestPage.xaml - you can see that there are TextBlocks with Text being bound to ValidationMessage properties of the input boxes. PatternErrorMessage defines what the message should be if the pattern is not matched. ValidationMessage is the first error that was found if one was found and ValidationMessageVisibility specifies whether the error UI should be visible.
<TextBlock
    Style="{StaticResource ValidationErrorLabelStyle}"
    Text="{Binding (extensions:FieldValidationExtensions.ValidationMessage), ElementName=NonEmptyTextBox}"
    Visibility="{Binding (extensions:FieldValidationExtensions.ValidationMessageVisibility), ElementName=NonEmptyTextBox}"/>
Jan 9, 2014 at 12:07 AM
Thank you. This helps a lot. The samples are really helpful. I should spend more time on them. I did not know that the source code has so many samples.

Hong
Jan 9, 2014 at 12:11 AM
I forgot to mention that I have been using the following code and it has been fine.
if (WinRTXamlToolkit.Controls.Extensions.FieldValidationExtensions.GetIsValid(tbEmail))
{
    //code to accept the input
}
else
{
   // code reject the input
}
Apparently binding a TextBlock to the message is a much better way.

Hong