Beschreibung:
Oft muss man verschiedene Benutzereingaben validieren. Diese Komponente vereinfacht das ein bisschen.
// Beispiel: Benutzereingabe soll ein Datum sein.
string input = HoleEingabeVomUniversum();
// Es soll ein Datum sein. Format ist entweder dd.mm.YYYY, dd.mm.YY oder dd. MMM. YYYY
IValidator val = Validators.AcceptOneOf(
Validators.AcceptFormat("##.##.####", "dd.mm.YYYY"), // 01.01.2000
Validators.AcceptFormat("##.##.##", "dd.mm.YY"), // 01.01.00
Validators.AcceptFormat("##. %%%. ####", "dd. MMM. YYYY"), // 01. Jan. 2000
);
// Die eingentliche Validation
ValidationResult res = val.Validate(input);
if (res.Success) {
// Wenn erfolgreich --> Mach irgendwas
MachIrgendwas((string) res.Value);
} else {
// Wenn nicht --> Zeige eine Fehlermeldung
Fehlermeldung(res.Error);
}
Die Standard-mäßig zur Verfügung stehenden Validatoren sind:
/// <summary>
/// Returns a validator which ensures that the input is between <paramref name="min"/> and <paramref name="max"/>.
/// </summary>
/// <param name="min">The lower bound (including min).</param>
/// <param name="max">The upper bound (including max).</param>
/// <returns>An instance of IValidator for validating ranges.</returns>
public static IValidator AcceptRange<T>(T min, T max) where T : IComparable<T>;
/// <summary>
/// Returns a validator which accepts every input.
/// </summary>
/// <returns>An instance of IValidator for validating everything.</returns>
public static IValidator AcceptEverything();
/// <summary>
/// Returns a validator which accepts non-empty strings.
/// </summary>
/// <returns>An instance of IValidator for validating non-empty strings.</returns>
public static IValidator AcceptNonEmptyStrings();
/// <summary>
/// Returns a validator which ensures that the format expression does match the input.
/// </summary>
/// <param name="format">
/// The format expression.
/// Format expression syntax:
/// "*" = Any number of chars (.*?),
/// "?" = A single character (.),
/// "#" = A single digit (\d),
/// "%" = A single letter (\w).
/// Other input characters will be escaped using Regex.Escape(...).
/// </param>
/// <param name="formatDescriptor">The format descriptor for the format expression (e.g. "dd-mm-YYYY").</param>
/// <returns>An instance of IValidator for validating against format expressions.</returns>
public static IValidator AcceptFormat(string format, string formatDescriptor);
/// <summary>
/// Returns a validator which ensures that the regular expression does match the input.
/// </summary>
/// <param name="regex">The regular expression.</param>
/// <param name="formatDescriptor">The format descriptor for the regular expression (e.g. "dd-mm-YYYY").</param>
/// <returns></returns>
public static IValidator AcceptFormat(Regex regex, string formatDescriptor);
/// <summary>
/// The input has to be successfully validated by one of the specified <paramref name="validators"/>.
/// </summary>
/// <param name="validators">The validators.</param>
/// <returns>An instance of IValidator for validating one of the specified <paramref name="validators"/>.</returns>
public static IValidator AcceptOneOf(params IValidator[] validators);
/// <summary>
/// The input has to be successfully validated by all of the specified <paramref name="validators"/>.
/// </summary>
/// <param name="validators">The validators.</param>
/// <returns>An instance of IValidator for validating all of the specified <paramref name="validators"/>.</returns>
public static IValidator AcceptUnionOf(params IValidator[] validators);
/// <summary>
/// Accepts IP version 4 addresses.
/// </summary>
/// <returns>An instance of IValidator for validating ÍPv4Addresses.</returns>
public static IValidator AcceptIPv4Address();
/// <summary>
/// Accepts URLs.
/// </summary>
/// <returns>An instance of IValidator for validating Urls.</returns>
public static IValidator AcceptUrl();
/// <summary>
/// Accepts host names or IP addresses with port.
/// </summary>
/// <returns>An instance of IValidator for validating host names.</returns>
public static IValidator AcceptHostName();
/// <summary>
/// Accepts timestamps.
/// </summary>
/// <returns>An instance of IValidator for validating timestamps.</returns>
public static IValidator AcceptTime();
/// <summary>
/// Accepts any numbers (integers AND floats).
/// </summary>
/// <returns>An instance of IValidator for validating numbers.</returns>
public static IValidator AcceptNumbers();
/// <summary>
/// Accepts only integer numbers.
/// </summary>
/// <returns>An instance of IValidator for validating integer numbers.</returns>
public static IValidator AcceptIntegerNumbers();
/// <summary>
/// Accepts only floating point numbers.
/// </summary>
/// <returns>An instance of for validating floating point numbers.</returns>
public static IValidator AcceptFloatNumbers();
Gruß, Christian.
Schlagwörter: Validation überprüfung eingabe input