162 lines
5.1 KiB
C#
162 lines
5.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Controls;
|
|
|
|
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"
|
|
};
|
|
|
|
public static ValidationResult CheckNumericInput(TextBox input)
|
|
{
|
|
return CheckNumericInput(input, -1);
|
|
}
|
|
|
|
private static ValidationResult CheckNumericInput(TextBox input, int maxLen)
|
|
{
|
|
string text = "";
|
|
int pos = input.CaretIndex;
|
|
for (int i = 0; i < input.Text.Length; i++)
|
|
{
|
|
char ch = input.Text[i];
|
|
if (Char.IsDigit(ch))
|
|
text += ch;
|
|
if (i == input.CaretIndex - 1)
|
|
pos = text.Length;
|
|
}
|
|
input.Text = text;
|
|
input.CaretIndex = pos;
|
|
|
|
if (maxLen >= 0 && input.Text.Length > maxLen)
|
|
{
|
|
input.Text = input.Text.Substring(0, maxLen);
|
|
input.CaretIndex = Math.Min(pos, maxLen);
|
|
}
|
|
|
|
return new(true, null);
|
|
}
|
|
|
|
public static ValidationResult CheckPhoneNumber(TextBox input)
|
|
{
|
|
string text = "";
|
|
int pos = input.CaretIndex;
|
|
for (int i = 0, v = 0; i < input.Text.Length; i++)
|
|
{
|
|
char ch = input.Text[i];
|
|
if (v == 0 && ch == '0')
|
|
{
|
|
v += 3;
|
|
text += "+43";
|
|
}
|
|
else if (v == 0 && ch == '+')
|
|
{
|
|
v++;
|
|
text += ch;
|
|
}
|
|
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 "))
|
|
text += " ";
|
|
v++;
|
|
text += ch;
|
|
}
|
|
if (i == input.CaretIndex - 1)
|
|
pos = text.Length;
|
|
}
|
|
input.Text = text;
|
|
input.CaretIndex = pos;
|
|
|
|
if (text.Length == 0)
|
|
return new(true, null);
|
|
if (text.Length < 10)
|
|
return new(false, "Telefonnummer zu kurz");
|
|
|
|
return new(true, null);
|
|
}
|
|
|
|
public static ValidationResult CheckEmailAddress(TextBox input)
|
|
{
|
|
string text = "";
|
|
int pos = input.CaretIndex;
|
|
bool domain = false;
|
|
for (int i = 0; i < input.Text.Length; i++)
|
|
{
|
|
char ch = input.Text[i];
|
|
if (domain)
|
|
{
|
|
if ((char.IsAscii(ch) && char.IsLetterOrDigit(ch)) || ".-_öäüßÖÄÜẞ".Any(c => c == ch))
|
|
{
|
|
if (!(text.Last() == '.' && ch == '.'))
|
|
text += char.ToLower(ch);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (ch == '@') domain = true;
|
|
if (!char.IsControl(ch) && !char.IsWhiteSpace(ch))
|
|
text += ch;
|
|
}
|
|
|
|
if (i == input.CaretIndex - 1)
|
|
pos = text.Length;
|
|
}
|
|
input.Text = text;
|
|
input.CaretIndex = pos;
|
|
|
|
if (text.Length == 0)
|
|
return new(true, null);
|
|
else if (text[0] == '@' || !domain)
|
|
return new(false, "E-Mail-Adresse ungültig");
|
|
var last = text.Split(".").Last();
|
|
if (last.Length < 2 || !last.All(ch => char.IsAscii(ch) && char.IsLower(ch)))
|
|
return new(false, "E-Mail-Adresse ungültig");
|
|
|
|
return new(true, null);
|
|
}
|
|
|
|
public static ValidationResult CheckLfbisNr(TextBox input)
|
|
{
|
|
var res = CheckNumericInput(input, 7);
|
|
if (!res.IsValid)
|
|
return res;
|
|
if (input.Text.Length == 0)
|
|
return new(true, null);
|
|
if (input.Text.Length != 7)
|
|
return new(false, "Betriebsnummer zu kurz");
|
|
|
|
// TODO
|
|
|
|
return new(true, "Not implemented yet");
|
|
}
|
|
|
|
public static ValidationResult CheckUstIdInput(TextBox input)
|
|
{
|
|
return new(false, "Not implemented yet");
|
|
}
|
|
|
|
public static void SetInputInvalid(TextBox input)
|
|
{
|
|
input.BorderBrush = System.Windows.Media.Brushes.Red;
|
|
}
|
|
|
|
public static void SetInputValid(TextBox input)
|
|
{
|
|
input.ClearValue(TextBox.BorderBrushProperty);
|
|
}
|
|
}
|
|
}
|