Implementing Regex

This commit is contained in:
francesco 2020-08-24 21:17:04 +02:00
parent d23f8cf465
commit 9b233de6a8
6 changed files with 138 additions and 12 deletions

View File

@ -117,7 +117,11 @@
<Compile Include="Repository\LocationRepository.cs" /> <Compile Include="Repository\LocationRepository.cs" />
<Compile Include="Repository\LoggingRepository.cs" /> <Compile Include="Repository\LoggingRepository.cs" />
<Compile Include="Repository\RepositoryBase.cs" /> <Compile Include="Repository\RepositoryBase.cs" />
<Compile Include="Validators\CustomerNumberValidationRule.cs" />
<Compile Include="Validators\EmailValidationRule.cs" />
<Compile Include="Validators\IntRangeValidationRule.cs" /> <Compile Include="Validators\IntRangeValidationRule.cs" />
<Compile Include="Validators\PasswordValidationRule.cs" />
<Compile Include="Validators\UrlValidationRule.cs" />
<Compile Include="ViewModel\Commands\BaseCommand.cs" /> <Compile Include="ViewModel\Commands\BaseCommand.cs" />
<Compile Include="ViewModel\Commands\RelayCommand.cs" /> <Compile Include="ViewModel\Commands\RelayCommand.cs" />
<Compile Include="Validators\StringRangeValidationRule.cs" /> <Compile Include="Validators\StringRangeValidationRule.cs" />

View File

@ -0,0 +1,32 @@
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Controls;
namespace LoggingClient.Validators
{
public class CustomerNumberValidationRule : ValidationRule
{
public string ErrorMessage { get; set; }
/// <summary>
/// Regex for Address Number Validation.
/// Beginning with CU following a 5 digit number.
/// </summary>
/// <param name="value"></param>
/// <param name="cultureInfo"></param>
/// <returns></returns>
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
Regex regex = new Regex(@"^CU[0-9]{5}$");
Match match = regex.Match(value.ToString());
if (match == Match.Empty)
{
return new ValidationResult(false, ErrorMessage);
}
else
{
return ValidationResult.ValidResult;
}
}
}
}

View File

@ -0,0 +1,31 @@
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Controls;
namespace LoggingClient.Validators
{
public class EmailValidationRule : ValidationRule
{
public string ErrorMessage { get; set; }
/// <summary>
/// Checks an Email Address if it's valid or not
/// Regex doesn't check if the top + subdomains are valid.
/// </summary>
/// <param name="value"></param>
/// <param name="cultureInfo"></param>
/// <returns></returns>
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
Regex regex = new Regex(@"\A[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z");
Match match = regex.Match(value.ToString());
if (match == Match.Empty)
{
return new ValidationResult(false, ErrorMessage);
}
else
{
return ValidationResult.ValidResult;
}
}
}
}

View File

@ -0,0 +1,31 @@
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Controls;
namespace LoggingClient.Validators
{
public class PasswordValidationRule : ValidationRule
{
public string ErrorMessage { get; set; }
/// <summary>
/// Regex for password validation
/// 8 - 15 characters. At least 1 upper, 1 lowercase, 1 number and 1 special character.
/// </summary>
/// <param name="value"></param>
/// <param name="cultureInfo"></param>
/// <returns></returns>
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
Regex regex = new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$");
Match match = regex.Match(value.ToString());
if (match == Match.Empty)
{
return new ValidationResult(false, ErrorMessage);
}
else
{
return ValidationResult.ValidResult;
}
}
}
}

View File

@ -0,0 +1,32 @@
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Controls;
namespace LoggingClient.Validators
{
public class UrlValidationRule : ValidationRule
{
public string ErrorMessage { get; set; }
/// <summary>
/// Regex for url Validation.
/// Doesn't check if the url exists.
/// </summary>
/// <param name="value"></param>
/// <param name="cultureInfo"></param>
/// <returns></returns>
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
Regex regex = new Regex(@"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$");
Match match = regex.Match(value.ToString());
if (match == Match.Empty)
{
return new ValidationResult(false, ErrorMessage);
}
else
{
return ValidationResult.ValidResult;
}
}
}
}

View File

@ -63,9 +63,8 @@
<TextBox.Text> <TextBox.Text>
<Binding ElementName="DataGridCustomer" Path="SelectedItem.customernumber" UpdateSourceTrigger="PropertyChanged"> <Binding ElementName="DataGridCustomer" Path="SelectedItem.customernumber" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules> <Binding.ValidationRules>
<validators:StringRangeValidationRule <validators:CustomerNumberValidationRule
MinimumLength="1" MaximumLength="45" ErrorMessage="CU12345 - Pattern of a valid CustomerNumber" />
ErrorMessage="Message must contain at least 1 character up to 45" />
</Binding.ValidationRules> </Binding.ValidationRules>
</Binding> </Binding>
</TextBox.Text> </TextBox.Text>
@ -75,9 +74,8 @@
<TextBox HorizontalAlignment="Left" Height="25" Margin="310,321,0,0" TextWrapping="Wrap" x:Name="EnterUrl" VerticalAlignment="Top" Width="95"> <TextBox HorizontalAlignment="Left" Height="25" Margin="310,321,0,0" TextWrapping="Wrap" x:Name="EnterUrl" VerticalAlignment="Top" Width="95">
<Binding ElementName="DataGridCustomer" Path="SelectedItem.url" UpdateSourceTrigger="PropertyChanged"> <Binding ElementName="DataGridCustomer" Path="SelectedItem.url" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules> <Binding.ValidationRules>
<validators:StringRangeValidationRule <validators:UrlValidationRule
MinimumLength="1" MaximumLength="30" ErrorMessage="Invalid URL." />
ErrorMessage="Message must contain at least 1 characters up to 30" />
</Binding.ValidationRules> </Binding.ValidationRules>
</Binding> </Binding>
</TextBox> </TextBox>
@ -111,9 +109,8 @@
<TextBox HorizontalAlignment="Left" Height="25" Margin="210,372,0,0" TextWrapping="Wrap" x:Name="EnterEmail" VerticalAlignment="Top" Width="95"> <TextBox HorizontalAlignment="Left" Height="25" Margin="210,372,0,0" TextWrapping="Wrap" x:Name="EnterEmail" VerticalAlignment="Top" Width="95">
<Binding ElementName="DataGridCustomer" Path="SelectedItem.email" UpdateSourceTrigger="PropertyChanged"> <Binding ElementName="DataGridCustomer" Path="SelectedItem.email" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules> <Binding.ValidationRules>
<validators:StringRangeValidationRule <validators:EmailValidationRule
MinimumLength="1" MaximumLength="30" ErrorMessage="Invalid Email Address" />
ErrorMessage="Message must contain at least 1 characters up to 30" />
</Binding.ValidationRules> </Binding.ValidationRules>
</Binding> </Binding>
</TextBox> </TextBox>
@ -122,9 +119,8 @@
<TextBox HorizontalAlignment="Left" Height="25" Margin="310,372,0,0" TextWrapping="Wrap" x:Name="EnterPassword" VerticalAlignment="Top" Width="95"> <TextBox HorizontalAlignment="Left" Height="25" Margin="310,372,0,0" TextWrapping="Wrap" x:Name="EnterPassword" VerticalAlignment="Top" Width="95">
<Binding ElementName="DataGridCustomer" Path="SelectedItem.password" UpdateSourceTrigger="PropertyChanged"> <Binding ElementName="DataGridCustomer" Path="SelectedItem.password" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules> <Binding.ValidationRules>
<validators:StringRangeValidationRule <validators:PasswordValidationRule
MinimumLength="1" MaximumLength="255" ErrorMessage="1 lower, 1 UPPER-case, 1 number, 1 SpecialCaracter, 8-15 caracters" />
ErrorMessage="Message must contain at least 1 character up to 255" />
</Binding.ValidationRules> </Binding.ValidationRules>
</Binding> </Binding>
</TextBox> </TextBox>