diff --git a/Elwig/Helpers/Validator.cs b/Elwig/Helpers/Validator.cs
index af0964c..4b402e7 100644
--- a/Elwig/Helpers/Validator.cs
+++ b/Elwig/Helpers/Validator.cs
@@ -345,13 +345,70 @@ namespace Elwig.Helpers {
}
public static ValidationResult CheckDate(TextBox input, bool required) {
- // TODO
- return new(true, "Not implemented yet");
+ return CheckDate(input, required, false);
}
public static ValidationResult CheckPartialDate(TextBox input, bool required) {
- // TODO
- return new(true, "Not implemented yet");
+ return CheckDate(input, required, true);
+ }
+
+ private static ValidationResult CheckDate(TextBox input, bool required, bool partial) {
+ string text = "";
+ int pos = input.CaretIndex;
+ int p = 0;
+ var parts = new string?[3];
+ parts[0] = "";
+ for (int i = 0; i < input.Text.Length; i++) {
+ char ch = input.Text[i];
+ if (ch == '.') {
+ if (p < 2 && (parts[p]?.Length > 0 || pos == text.Length)) {
+ if (parts[p]?.Length == 1 && ((pos != text.Length && pos != text.Length - 1) || !input.IsFocused)) {
+ parts[p] = "0" + parts[p];
+ text = text[..(text.Length - 1)] + "0" + text[^1];
+ }
+ parts[++p] = "";
+ text += '.';
+ }
+ } else if (char.IsAsciiDigit(ch)) {
+ if ((partial && parts[p]?.Length < 4) || (!partial && ((p < 2 && parts[p]?.Length < 2) || (p == 2 && parts[2]?.Length < 4)))) {
+ parts[p] += ch;
+ text += ch;
+ }
+ if (p == 0 && pos == 1 && input.Text.Length >= 4 && input.Text.Length <= 9) {
+ parts[++p] = "";
+ text += '.';
+ continue; // skip caret update
+ }
+ }
+
+ if (i == input.CaretIndex - 1) {
+ pos = text.Length;
+ }
+ }
+
+ input.Text = text;
+ input.CaretIndex = pos;
+
+ if (text.Length == 0)
+ return required ? new(false, "Datum ist nicht optional") : new(true, null);
+
+ if (partial) {
+ if (p == 0) {
+ // only year provided
+ return (parts[0]?.Length == 4) ? new(true, null) : new(false, "Datum ist ungültig");
+ } else if (p == 1) {
+ // only month and year provided
+ int m = parts[0] != null && parts[0] != "" ? int.Parse(parts[0] ?? "0") : 0;
+ if (parts[1]?.Length != 4 || parts[0]?.Length != 2 || m < 1 || m > 12)
+ return new(false, "Datum ist ungültig");
+ return new(true, null);
+ }
+ }
+
+ if (!DateOnly.TryParseExact(text, "dd.MM.yyyy", out _))
+ return new(false, "Datum ist ungültig");
+
+ return new(true, null);
}
public static ValidationResult CheckVNr(TextBox input, bool required, AppDbContext ctx, Contract? c) {
diff --git a/Elwig/Windows/AdministrationWindow.cs b/Elwig/Windows/AdministrationWindow.cs
index 45dd391..faee310 100644
--- a/Elwig/Windows/AdministrationWindow.cs
+++ b/Elwig/Windows/AdministrationWindow.cs
@@ -258,10 +258,18 @@ namespace Elwig.Windows {
InputTextChanged((TextBox)sender, Validator.CheckPartialDate);
}
+ protected void PartialDateInput_LostFocus(object sender, RoutedEventArgs evt) {
+ InputLostFocus((TextBox)sender, Validator.CheckPartialDate);
+ }
+
protected void DateInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, Validator.CheckDate);
}
+ protected void DateInput_LostFocus(object sender, RoutedEventArgs evt) {
+ InputLostFocus((TextBox)sender, Validator.CheckDate);
+ }
+
protected void PlzInput_TextChanged(object sender, RoutedEventArgs evt) {
var plz = (TextBox)sender;
InputTextChanged(plz, Validator.CheckPlz);
diff --git a/Elwig/Windows/MainWindow.xaml.cs b/Elwig/Windows/MainWindow.xaml.cs
index 51e932c..aba7ccc 100644
--- a/Elwig/Windows/MainWindow.xaml.cs
+++ b/Elwig/Windows/MainWindow.xaml.cs
@@ -1,11 +1,5 @@
-using Microsoft.EntityFrameworkCore;
-using System;
-using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
-using System.Runtime.Intrinsics.X86;
-using System.Text;
-using System.Threading.Tasks;
using System.Windows;
using Elwig.Documents;
using Elwig.Helpers;
diff --git a/Elwig/Windows/MemberAdminWindow.xaml b/Elwig/Windows/MemberAdminWindow.xaml
index 8b3bf08..da1ae74 100644
--- a/Elwig/Windows/MemberAdminWindow.xaml
+++ b/Elwig/Windows/MemberAdminWindow.xaml
@@ -140,7 +140,7 @@
+ TextChanged="PartialDateInput_TextChanged" LostFocus="PartialDateInput_LostFocus"/>
+ TextChanged="DateInput_TextChanged" LostFocus="DateInput_LostFocus"/>
+ TextChanged="DateInput_TextChanged" LostFocus="DateInput_LostFocus"/>