Skip to content

Commit

Permalink
simple project name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
grzybeek committed Mar 31, 2024
1 parent 9eb23cb commit 6c8a443
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
12 changes: 6 additions & 6 deletions grzyClothTool/Views/BuildWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
</GroupBox>

<c:ModernLabelTextBox
x:Name="name"
Label="Project name"
Margin="15,15,15,0"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
Text="{Binding ProjectName}" />
x:Name="name"
Label="Project name"
Margin="15,15,15,0"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
Text="{Binding ProjectName}" />

<c:ModernLabelTextBox
x:Name="output"
Expand Down
33 changes: 33 additions & 0 deletions grzyClothTool/Views/BuildWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
Expand Down Expand Up @@ -40,6 +41,13 @@ private void Window_MouseDown(object sender, MouseButtonEventArgs e)

private async void build_MyBtnClickEvent(object sender, RoutedEventArgs e)
{
var error = ValidateProjectName();
if (error != null)
{
MessageBox.Show(error);
return;
}

if (ProjectName == null || BuildPath == null)
{
MessageBox.Show("Please fill in all fields");
Expand Down Expand Up @@ -121,5 +129,30 @@ private void RadioButton_ChangedEvent(object sender, RoutedEventArgs e)
};
}
}

private string ValidateProjectName()
{
string result = null;

if (string.IsNullOrEmpty(ProjectName))
{
result = "Project name cannot be empty";
}
else if (ProjectName.Length < 3)
{
result = "Project name must be at least 3 characters long";
}
else if (ProjectName.Length > 50)
{
result = "Project name cannot be longer than 50 characters";
}
else if (!Regex.IsMatch(ProjectName, @"^[a-z0-9_]+$"))
{
result = "Project name can only contain lowercase letters, numbers, and underscores";

}

return result;
}
}
}

0 comments on commit 6c8a443

Please sign in to comment.