-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from 10 commits
58833ab
47a78b2
7e971fb
a373dad
0082fa3
e3f4e5c
a2ef662
536639d
1d52c76
769e2a9
58fd753
d633ddf
ac2c96b
17e2b77
ea4e3c6
18fa31e
3ced3c1
ead9f06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -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" | ||
HeaderTemplate="{StaticResource HeaderTemplate}" | ||
Style="{StaticResource TextBoxRegexExStyle}" | ||
Text="" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty text setter |
||
<StackPanel Orientation="Horizontal"> | ||
<TextBlock Text="Validation Result: " /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix spacing before and after PhoneNumber There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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="" /> | ||
<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,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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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."