Validate email for member models (#17532)
* Validate email for member models * Add null check or more test cases * return invalid when not a valid email * Cleanup * remove private method in favor of extension * Remove non used, using statement --------- Co-authored-by: Elitsa <elm@umbraco.dk>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
@@ -1558,6 +1559,14 @@ public static class StringExtensions
|
||||
yield return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a string is a valid email address.
|
||||
/// </summary>
|
||||
/// <param name="email">The string check</param>
|
||||
/// <returns>Returns a bool indicating whether the string is an email address.</returns>
|
||||
public static bool IsEmail(this string? email) =>
|
||||
string.IsNullOrWhiteSpace(email) is false && new EmailAddressAttribute().IsValid(email);
|
||||
|
||||
// having benchmarked various solutions (incl. for/foreach, split and LINQ based ones),
|
||||
// this is by far the fastest way to find string needles in a string haystack
|
||||
public static int CountOccurrences(this string haystack, string needle)
|
||||
|
||||
Reference in New Issue
Block a user