Sprache: C#
Diese Methode validiert einen MD5 String.
Sind die einzelnen Hexwerte mit einem – getrennt, kann folgender Pattern verwendet werden:
^([a-fA-F0-9]{2}-){15}[a-fA-F0-9]{2}$
Benötigter Namespace:
System.Text.RegularExpressions
/// <summary>
/// Determines whether [is valid M d5] [the specified candidate].
/// </summary>
/// <param name="candidate">The candidate.</param>
/// <returns>
/// <c>true</c> if [is valid M d5] [the specified candidate]; otherwise, <c>false</c>.
/// </returns>
public static bool IsValidMD5(string candidate)
{
if (string.IsNullOrEmpty(candidate))
return false;
Regex isMD5 = new Regex(@"^([a-fA-F0-9]){32}$");
return isMD5.IsMatch(candidate);
}
/// <summary>
/// Determines whether [is valid M d5] [the specified candidate].
/// </summary>
/// <param name="candidate">The candidate.</param>
/// <returns>
/// <c>true</c> if [is valid M d5] [the specified candidate]; otherwise, <c>false</c>.
/// </returns>
public static bool IsValidMD5(string candidate)
{
if (string.IsNullOrEmpty(candidate))
return false;
Regex isMD5 = new Regex(@"^([a-fA-F0-9]){32}$");
return isMD5.IsMatch(candidate);
}
Alte URL:
/snippet/md5-string-validieren/466
Wenn es geht würde ich versuchen Regex zu vermeiden. 2 Möglichkeiten zeige ich hier: http://dotnet-snippets.de/snippet/md5-validieren/14107
Bindestriche dürfen sie in dem Fall nicht enthalten, habe ich aber auch noch nicht gesehen. Ansonsten kann man meine Snippets entsprechend ergänzen.