Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextBox Regex Extension #799

Merged
merged 18 commits into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Microsoft.Toolkit.Uwp.SampleApp/Common/BoolStringConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************

using System;
using Windows.UI.Xaml.Data;

namespace Microsoft.Toolkit.Uwp.SampleApp.Common
{
internal class BoolStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return value.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return bool.Parse(value.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
<Content Include="SamplePages\Object Storage\ObjectStorage.png" />
<Content Include="SamplePages\SurfaceDialTextboxHelper\SurfaceDialTextboxHelper.png" />
<Content Include="SamplePages\TextBoxMask\TextBoxMask.png" />
<Content Include="SamplePages\TextBoxRegexEx\TextBoxRegexEx.png" />
<Content Include="SamplePages\Toast\icon.jpg" />
<Content Include="SamplePages\Twitter Service\TwitterCode.bind" />
<Content Include="SamplePages\Twitter Service\icon.png" />
Expand Down Expand Up @@ -349,11 +350,13 @@
<Content Include="SamplePages\MasterDetailsView\MasterDetailsViewCode.bind" />
<Content Include="SamplePages\Microsoft Translator Service\MicrosoftTranslatorCode.bind" />
<Content Include="SamplePages\AdvancedCollectionView\AdvancedCollectionView.bind" />
<Content Include="SamplePages\TextBoxRegexEx\TextBoxRegexEx.bind" />
</ItemGroup>
<ItemGroup>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Common\BoolStringConverter.cs" />
<Compile Include="Common\Constants.cs" />
<Compile Include="Common\DelegateCommand{T}.cs" />
<Compile Include="Common\EnumConverter.cs" />
Expand Down Expand Up @@ -465,6 +468,9 @@
<Compile Include="SamplePages\TextBoxMask\TextBoxMaskPage.xaml.cs">
<DependentUpon>TextBoxMaskPage.xaml</DependentUpon>
</Compile>
<Compile Include="SamplePages\TextBoxRegexEx\TextBoxRegexExPage.xaml.cs">
<DependentUpon>TextBoxRegexExPage.xaml</DependentUpon>
</Compile>
<Compile Include="SamplePages\Toast\ToastPage.xaml.cs">
<DependentUpon>ToastPage.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -669,6 +675,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="SamplePages\TextBoxRegexEx\TextBoxRegexExPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="SamplePages\Toast\ToastPage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<Page x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.TextBoxRegexExPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:common="using:Microsoft.Toolkit.Uwp.SampleApp.Common"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Page.Resources>
<common:BoolStringConverter x:Key="StringFormatConverter" />
<Style x:Key="TextBoxRegexExStyle"
TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
<DataTemplate x:Key="HeaderTemplate">
<StackPanel>
<TextBlock Text="{Binding}"
TextWrapping="WrapWholeWords" />
</StackPanel>
</DataTemplate>
</Page.Resources>

<Grid Background="{StaticResource Brush-Grey-05}">
<Grid Margin="30">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>

<StackPanel Margin="10,0,10,0">
<TextBox Name="PhoneNumberValidator"
controls:TextBoxRegexEx.Regex="^\s*\+?\s*([0-9][\s-]*){9,}$"
Header="Text box with Regex extension for phone number, validation occurs on textbox changed"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "validation occurs on TextChanged."

HeaderTemplate="{StaticResource HeaderTemplate}"
Style="{StaticResource TextBoxRegexExStyle}"
Text="" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the empty text setter

<StackPanel Orientation="Horizontal">
<TextBlock Text="Validation Result: " />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the text to "Is Valid", or "Valid Value" or similar. With "Validation Result" I would expect values of "Valid" and "Invalid"

<TextBlock Text="{Binding (controls:TextBoxRegexEx.IsValid), ElementName=PhoneNumberValidator, Converter={StaticResource StringFormatConverter}}" />
</StackPanel>

</StackPanel>

<StackPanel Grid.Row="1"
Margin="10,0,10,0">
<TextBox Name="PhoneNumberValidatorForce"
controls:TextBoxRegexEx.ValidationMode="Forced"
controls:TextBoxRegexEx.ValidationType="PhoneNumber"
Header="Text box with ValidationType= PhoneNumber , validation occurs on textbox changed and force occurs on lose focus with ValidationMode=Force"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix spacing before and after PhoneNumber

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "validation occurs on TextChanged and"

HeaderTemplate="{StaticResource HeaderTemplate}"
Style="{StaticResource TextBoxRegexExStyle}"
Text="+61616161611" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Validation Result: " />
<TextBlock Text="{Binding (controls:TextBoxRegexEx.IsValid), ElementName=PhoneNumberValidatorForce, Converter={StaticResource StringFormatConverter}}" />
</StackPanel>
</StackPanel>

<StackPanel Grid.Row="2"
Margin="10,0,10,0">
<TextBox Name="EmailValidatorForce"
controls:TextBoxRegexEx.ValidationType="Email"
Header="Text box with ValidationType=Email, validation occurs on textbox changed"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "validation occurs on TextChanged."
Change any others

HeaderTemplate="{StaticResource HeaderTemplate}"
Style="{StaticResource TextBoxRegexExStyle}"
Text="" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Validation Result: " />
<TextBlock Text="{Binding (controls:TextBoxRegexEx.IsValid), ElementName=EmailValidatorForce, Converter={StaticResource StringFormatConverter}}" />
</StackPanel>
</StackPanel>

<StackPanel Grid.Row="3"
Margin="10,0,10,0">
<TextBox Name="DecimalValidatorForce"
controls:TextBoxRegexEx.ValidationMode="Forced"
controls:TextBoxRegexEx.ValidationType="Decimal"
Header="Text box with ValidationType=Decimal, validation occurs on textbox changed and force occurs on lose focus with ValidationMode=Force (333,111 or 333.111)"
HeaderTemplate="{StaticResource HeaderTemplate}"
Style="{StaticResource TextBoxRegexExStyle}"
Text="" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Validation Result: " />
<TextBlock Text="{Binding (controls:TextBoxRegexEx.IsValid), ElementName=DecimalValidatorForce, Converter={StaticResource StringFormatConverter}}" />
</StackPanel>
</StackPanel>

</Grid>
</Grid>
</Page>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<Page x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.TextBoxRegexExPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:common="using:Microsoft.Toolkit.Uwp.SampleApp.Common"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Page.Resources>
<common:BoolStringConverter x:Key="StringFormatConverter" />
<Style x:Key="TextBoxRegexExStyle"
TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
<DataTemplate x:Key="HeaderTemplate">
<StackPanel>
<TextBlock Text="{Binding}"
TextWrapping="WrapWholeWords" />
</StackPanel>
</DataTemplate>
</Page.Resources>

<Grid Background="{StaticResource Brush-Grey-05}">
<Grid Margin="30">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>

<StackPanel Margin="10,0,10,0">
<TextBox Name="PhoneNumberValidator"
controls:TextBoxRegexEx.Regex="^\s*\+?\s*([0-9][\s-]*){9,}$"
Header="Text box with Regex extension for phone number, validation occurs on textbox changed"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments from the bind file

HeaderTemplate="{StaticResource HeaderTemplate}"
Style="{StaticResource TextBoxRegexExStyle}"
Text="" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Validation Result: " />
<TextBlock Text="{Binding (controls:TextBoxRegexEx.IsValid), ElementName=PhoneNumberValidator, Converter={StaticResource StringFormatConverter}}" />
</StackPanel>

</StackPanel>

<StackPanel Grid.Row="1"
Margin="10,0,10,0">
<TextBox Name="PhoneNumberValidatorForce"
controls:TextBoxRegexEx.ValidationMode="Forced"
controls:TextBoxRegexEx.ValidationType="PhoneNumber"
Header="Text box with ValidationType= PhoneNumber , validation occurs on textbox changed and force occurs on lose focus with ValidationMode=Force"
HeaderTemplate="{StaticResource HeaderTemplate}"
Style="{StaticResource TextBoxRegexExStyle}"
Text="+61616161611" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Validation Result: " />
<TextBlock Text="{Binding (controls:TextBoxRegexEx.IsValid), ElementName=PhoneNumberValidatorForce, Converter={StaticResource StringFormatConverter}}" />
</StackPanel>
</StackPanel>

<StackPanel Grid.Row="2"
Margin="10,0,10,0">
<TextBox Name="EmailValidatorForce"
controls:TextBoxRegexEx.ValidationType="Email"
Header="Text box with ValidationType=Email, validation occurs on textbox changed"
HeaderTemplate="{StaticResource HeaderTemplate}"
Style="{StaticResource TextBoxRegexExStyle}"
Text="" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Validation Result: " />
<TextBlock Text="{Binding (controls:TextBoxRegexEx.IsValid), ElementName=EmailValidatorForce, Converter={StaticResource StringFormatConverter}}" />
</StackPanel>
</StackPanel>

<StackPanel Grid.Row="3"
Margin="10,0,10,0">
<TextBox Name="DecimalValidatorForce"
controls:TextBoxRegexEx.ValidationMode="Forced"
controls:TextBoxRegexEx.ValidationType="Decimal"
Header="Text box with ValidationType=Decimal, validation occurs on textbox changed and force occurs on lose focus with ValidationMode=Force (333,111 or 333.111)"
HeaderTemplate="{StaticResource HeaderTemplate}"
Style="{StaticResource TextBoxRegexExStyle}"
Text="" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Validation Result: " />
<TextBlock Text="{Binding (controls:TextBoxRegexEx.IsValid), ElementName=DecimalValidatorForce, Converter={StaticResource StringFormatConverter}}" />
</StackPanel>
</StackPanel>

</Grid>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************

using Windows.UI.Xaml.Controls;

namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
{
/// <summary>
/// TextBox Regex Extension sample page
/// </summary>
public sealed partial class TextBoxRegexExPage : Page
{
public TextBoxRegexExPage()
{
InitializeComponent();
}
}
}
8 changes: 8 additions & 0 deletions Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@
"CodeUrl": "https://github.com/Microsoft/UWPCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI.Controls/WrapPanel",
"XamlCodeFile": "WrapPanel.bind",
"Icon": "/SamplePages/WrapPanel/WrapPanel.png"
},
{
"Name": "TextBoxRegexEx",
"Type": "TextBoxRegexExPage",
"About": "Textbox regex extension helps developer to validate a textbox with a regex using the Regex property.",
"CodeUrl": "https://github.com/Microsoft/UWPCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI.Controls/TextBoxRegexEx",
"XamlCodeFile": "TextBoxRegexEx.bind",
"Icon": "/SamplePages/TextBoxRegexEx/TextBoxRegexEx.png"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
<Compile Include="SurfaceDialTextboxHelper\SurfaceDialTextboxHelper.cs" />
<Compile Include="TextBoxMask\TextBoxMask.cs" />
<Compile Include="TextBoxMask\TextBoxMask.Properties.cs" />
<Compile Include="TextBoxRegexEx\TextBoxRegexEx.Data.cs" />
<Compile Include="TextBoxRegexEx\TextBoxRegexEx.Properties.cs" />
<Compile Include="TextBoxRegexEx\TextBoxRegexEx.cs" />
<Compile Include="WrapPanel\WrapPanel.cs" />
<Compile Include="WrapPanel\WrapPanel.Data.cs" />
<None Include="Microsoft.Toolkit.Uwp.UI.Controls.ruleset" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************

namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// Textbox regex extension helps developer to validate a textbox with a regex using the Regex property, IsValid property is updated with the regex validation result, if ValidationMode is Normal only IsValid property is setted if ValidationMode is Forced and the input is not valid the textbox text will be cleared
/// </summary>
public partial class TextBoxRegexEx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to TextBoxRegex

{
/// <summary>
/// Regex extension validation mode
/// </summary>
public enum ValidationMode
{
/// <summary>
/// Update IsValid property with validation result
/// </summary>
Normal,

/// <summary>
/// Update IsValid proeprty with validation result and in case the textbox is not valid clear its value
/// </summary>
Forced
}

/// <summary>
/// Specify the type of validation required
/// </summary>
public enum ValidationType
{
/// <summary>
/// The default validation that required property Regex to be setted
/// </summary>
Custom,

/// <summary>
/// Email validation
/// </summary>
Email,

/// <summary>
/// Number validation
/// </summary>
Number,

/// <summary>
/// Decimal validation
/// </summary>
Decimal,

/// <summary>
/// Text only validation
/// </summary>
Characters,

/// <summary>
/// Phone number validation
/// </summary>
PhoneNumber
}
}
}
Loading