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) {
|
||||
|
Reference in New Issue
Block a user