Add better TelNr validation

This commit is contained in:
2023-02-27 15:31:04 +01:00
parent 4be27ee5ff
commit 70b63e3dd4

View File

@ -10,11 +10,31 @@ namespace WGneu
{
static class Validator
{
private static readonly string[] MOBILE_NRS = {
private static readonly Dictionary<string, string[][]> PHONE_NRS = new() {
{ "43", new string[][] {
Array.Empty<string>(),
new string[] { "57", "59" },
new string[] {
"50", "517", "718", "804", "720", "780", "800", "802", "810",
"820", "821", "828", "900", "901", "930", "931", "939",
"650", "651", "652", "653", "655", "657", "659", "660", "661",
"663", "664", "665", "666", "667", "668", "669", "67", "68", "69"
}
} },
{ "49", Array.Empty<string[]>() },
{ "48", Array.Empty<string[]>() },
{ "420", Array.Empty<string[]>() },
{ "421", Array.Empty<string[]>() },
{ "36", Array.Empty<string[]>() },
{ "386", Array.Empty<string[]>() },
{ "39", Array.Empty<string[]>() },
{ "33", Array.Empty<string[]>() },
{ "41", Array.Empty<string[]>() },
{ "423", Array.Empty<string[]>() },
};
public static ValidationResult CheckNumericInput(TextBox input)
{
return CheckNumericInput(input, -1);
@ -51,7 +71,13 @@ namespace WGneu
for (int i = 0, v = 0; i < input.Text.Length; i++)
{
char ch = input.Text[i];
if (v == 0 && ch == '0')
if (v == 0 && input.Text.Length - i >= 2 && ch == '0' && input.Text[i + 1] == '0') {
v++; i++;
text += "+";
} else if (ch == '(' && input.Text.Length - i >= 3 && input.Text[i + 1] == '0' && input.Text[i + 2] == ')') {
i += 2;
}
else if (v == 0 && ch == '0')
{
v += 3;
text += "+43";
@ -63,14 +89,18 @@ namespace WGneu
}
else if (v > 0 && char.IsDigit(ch))
{
if (text == "+43")
if (PHONE_NRS.Any(kv => text == "+" + kv.Key))
text += " ";
else if (v == 6 && MOBILE_NRS.Any(nr => text.StartsWith("+43 " + nr)))
if (text.StartsWith("+43 ")) {
var nr = text[4..];
var vws = PHONE_NRS["43"];
if (v >= 4 && v - 4 < vws.Length && vws[v - 4].Any(vw => nr.StartsWith(vw)))
text += " ";
else if (v == 4 && text.StartsWith("+43 1"))
else if (nr == "1")
text += " ";
else if (v == 7 && text.Length == 8 && text.StartsWith("+43 "))
else if (v == 7 && nr.Length == 4)
text += " ";
}
v++;
text += ch;
}