Implement date validators
This commit is contained in:
@ -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) {
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -140,7 +140,7 @@
|
||||
|
||||
<Label Content="Geburtstag:" Margin="10,100,0,0" Grid.Column="0"/>
|
||||
<TextBox x:Name="BirthdayInput" Margin="0,100,0,0" Grid.Column="1" Width="78" TextAlignment="Right" HorizontalAlignment="Left"
|
||||
TextChanged="PartialDateInput_TextChanged"/>
|
||||
TextChanged="PartialDateInput_TextChanged" LostFocus="PartialDateInput_LostFocus"/>
|
||||
|
||||
<Label Content="Adresse:" Margin="10,130,0,0"/>
|
||||
<TextBox x:Name="AddressInput" Margin="0,130,10,0" Grid.Column="1" Grid.ColumnSpan="3"
|
||||
@ -248,11 +248,11 @@
|
||||
|
||||
<Label Content="Eintritt:" Margin="10,10,0,0" Grid.Column="0"/>
|
||||
<TextBox x:Name="EntryDateInput" Margin="0,10,10,0" Width="78" Grid.Column="1" HorizontalAlignment="Left" TextAlignment="Right"
|
||||
TextChanged="DateInput_TextChanged"/>
|
||||
TextChanged="DateInput_TextChanged" LostFocus="DateInput_LostFocus"/>
|
||||
|
||||
<Label Content="Austritt:" Margin="10,40,0,0" Grid.Column="0"/>
|
||||
<TextBox x:Name="ExitDateInput" Margin="0,40,10,0" Width="78" Grid.Column="1" HorizontalAlignment="Left" TextAlignment="Right"
|
||||
TextChanged="DateInput_TextChanged"/>
|
||||
TextChanged="DateInput_TextChanged" LostFocus="DateInput_LostFocus"/>
|
||||
|
||||
<Label Content="Geschäftsanteile:" Margin="10,70,0,0" Grid.Column="0"/>
|
||||
<TextBox x:Name="BusinessSharesInput" Margin="0,70,10,0" Width="48" Grid.Column="1" HorizontalAlignment="Left" TextAlignment="Right"
|
||||
|
Reference in New Issue
Block a user