From 70b63e3dd4d3001f785e67eef2f716c062714886 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Mon, 27 Feb 2023 15:31:04 +0100 Subject: [PATCH] Add better TelNr validation --- WGneu/Validator.cs | 52 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/WGneu/Validator.cs b/WGneu/Validator.cs index c000274..22413dd 100644 --- a/WGneu/Validator.cs +++ b/WGneu/Validator.cs @@ -10,11 +10,31 @@ namespace WGneu { static class Validator { - private static readonly string[] MOBILE_NRS = { - "650", "651", "652", "653", "655", "657", "659", "660", "661", - "663", "664", "665", "666", "667", "668", "669", "67", "68", "69" + + private static readonly Dictionary PHONE_NRS = new() { + { "43", new string[][] { + Array.Empty(), + 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() }, + { "48", Array.Empty() }, + { "420", Array.Empty() }, + { "421", Array.Empty() }, + { "36", Array.Empty() }, + { "386", Array.Empty() }, + { "39", Array.Empty() }, + { "33", Array.Empty() }, + { "41", Array.Empty() }, + { "423", Array.Empty() }, }; + 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") - text += " "; - else if (v == 6 && MOBILE_NRS.Any(nr => text.StartsWith("+43 " + nr))) - text += " "; - else if (v == 4 && text.StartsWith("+43 1")) - text += " "; - else if (v == 7 && text.Length == 8 && text.StartsWith("+43 ")) + if (PHONE_NRS.Any(kv => text == "+" + kv.Key)) text += " "; + 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 (nr == "1") + text += " "; + else if (v == 7 && nr.Length == 4) + text += " "; + } v++; text += ch; }