-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 ?

Hammas
  • 1,126
  • 1
  • 12
  • 37
  • 1
    Do you use it as in the docs? `extensions:TextBoxRegex.Regex="^(?!\s*$).+"`? – Wiktor Stribiżew Jul 10 '20 at 07:49
  • `xmlns:ext="using:Microsoft.Toolkit.Uwp.UI.Extensions"` yeah same way except i'm using `ext` – Hammas Jul 10 '20 at 07:53
  • and idk who down voted the question without even saying a single word :( – Hammas Jul 10 '20 at 07:54
  • What are you trying to match with your `^(?!\s*$).+` RegEx? It is kinda odd to me. – Cleptus Jul 10 '20 at 07:57
  • @bradbury9 sir. from this answer https://stackoverflow.com/questions/7967075/regex-for-not-empty-and-not-whitespace i got this regex to see if text box text is empty or null – Hammas Jul 10 '20 at 08:01
  • @bradbury9 idk if you got me or not but that's how it should work in case of Comunity toolkit for Uwp for text box text validation. – Hammas Jul 10 '20 at 08:01
  • So you didn't use it as in the docs. You must have included the regex delimiters into the regex pattern. The validation is working. – Wiktor Stribiżew Jul 10 '20 at 19:22

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Richard Zhang
  • 7,523
  • 1
  • 7
  • 13