-1

This is an example of custom regex validation for numbers only that is provided in official doc of Windows Community Toolkit of UWP here TextBox Regex extensions in toolkit.

<StackPanel Margin="10,0,10,0">
    <TextBox Name="PhoneNumberValidator"
             extensions:TextBoxRegex.Regex="^\s*\+?\s*([0-9][\s-]*){9,}$"
             Header="Text box with Regex extension for phone number, validation occurs on TextChanged"
             HeaderTemplate="{StaticResource HeaderTemplate}"
             Style="{StaticResource TextBoxRegexStyle}" />
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Is Valid: " />
        <TextBlock Text="{Binding (extensions:TextBoxRegex.IsValid), ElementName=PhoneNumberValidator, Converter={StaticResource StringFormatConverter}}" />
    </StackPanel>

</StackPanel>  

It works but if i change the regex and use a custom regex /^(?!\s*$).+/ for NotNullOrEmpty validation then it doesn't work. mean ext:TextBoxRegex.IsValid always returns false. Why ?

Rao official
  • 977
  • 1
  • 10
  • 33

1 Answers1

0

I tested the regular expression you provided in the WindowsCommunityToolkit Sample App:

<TextBox extensions:TextBoxRegex.Regex="(?!^$)([^\s])"
         .../>

It works fine, that is, after entering text, it shows the result as True.

Please note that the regular expression itself does not contain the first and last /, it is a syntax that indicates that the internal is a regular expression. When entering the relevant regular expression in XML, you only need to enter the content.

Thanks.

Richard Zhang
  • 7,217
  • 1
  • 6
  • 12