Skip to content

Commit 6c8a443

Browse files
committed
simple project name validation
1 parent 9eb23cb commit 6c8a443

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

grzyClothTool/Views/BuildWindow.xaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
</GroupBox>
3939

4040
<c:ModernLabelTextBox
41-
x:Name="name"
42-
Label="Project name"
43-
Margin="15,15,15,0"
44-
VerticalAlignment="Top"
45-
HorizontalAlignment="Stretch"
46-
Text="{Binding ProjectName}" />
41+
x:Name="name"
42+
Label="Project name"
43+
Margin="15,15,15,0"
44+
VerticalAlignment="Top"
45+
HorizontalAlignment="Stretch"
46+
Text="{Binding ProjectName}" />
4747

4848
<c:ModernLabelTextBox
4949
x:Name="output"

grzyClothTool/Views/BuildWindow.xaml.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.IO;
7+
using System.Text.RegularExpressions;
78
using System.Threading.Tasks;
89
using System.Windows;
910
using System.Windows.Input;
@@ -40,6 +41,13 @@ private void Window_MouseDown(object sender, MouseButtonEventArgs e)
4041

4142
private async void build_MyBtnClickEvent(object sender, RoutedEventArgs e)
4243
{
44+
var error = ValidateProjectName();
45+
if (error != null)
46+
{
47+
MessageBox.Show(error);
48+
return;
49+
}
50+
4351
if (ProjectName == null || BuildPath == null)
4452
{
4553
MessageBox.Show("Please fill in all fields");
@@ -121,5 +129,30 @@ private void RadioButton_ChangedEvent(object sender, RoutedEventArgs e)
121129
};
122130
}
123131
}
132+
133+
private string ValidateProjectName()
134+
{
135+
string result = null;
136+
137+
if (string.IsNullOrEmpty(ProjectName))
138+
{
139+
result = "Project name cannot be empty";
140+
}
141+
else if (ProjectName.Length < 3)
142+
{
143+
result = "Project name must be at least 3 characters long";
144+
}
145+
else if (ProjectName.Length > 50)
146+
{
147+
result = "Project name cannot be longer than 50 characters";
148+
}
149+
else if (!Regex.IsMatch(ProjectName, @"^[a-z0-9_]+$"))
150+
{
151+
result = "Project name can only contain lowercase letters, numbers, and underscores";
152+
153+
}
154+
155+
return result;
156+
}
124157
}
125158
}

0 commit comments

Comments
 (0)