-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidateExtensions.cs
More file actions
50 lines (42 loc) · 2.81 KB
/
ValidateExtensions.cs
File metadata and controls
50 lines (42 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Text.RegularExpressions;
namespace Netcorext.Extensions.Commons;
public static class ValidateExtensions
{
#region IsEmpty
public static bool IsEmpty(this string? source) => string.IsNullOrWhiteSpace(source);
public static bool IsEmpty(this int source) => source == 0;
public static bool IsEmpty(this int? source) => source is null or 0;
public static bool IsEmpty(this long source) => source == 0;
public static bool IsEmpty(this long? source) => source is null or 0;
public static bool IsEmpty(this double source) => source == 0;
public static bool IsEmpty(this double? source) => source is null or 0;
public static bool IsEmpty(this float source) => source == 0;
public static bool IsEmpty(this float? source) => source is null or 0;
public static bool IsEmpty(this decimal source) => source == 0;
public static bool IsEmpty(this decimal? source) => source is null or 0;
public static bool IsEmpty(this DateTime source) => source == default;
public static bool IsEmpty(this DateTime? source) => source is null || source == DateTime.MinValue;
public static bool IsEmpty(this DateTimeOffset source) => source == default;
public static bool IsEmpty(this DateTimeOffset? source) => source is null || source == DateTimeOffset.MinValue;
public static bool IsEmpty(this TimeSpan source) => source.TotalMilliseconds == 0;
public static bool IsEmpty(this TimeSpan? source) => source is null || source.Value.TotalMilliseconds == 0;
#endregion
#region IsNull
public static bool IsNull(this string? source) => source == null;
public static bool IsNull(this int? source) => source == null;
public static bool IsNull(this long? source) => source == null;
public static bool IsNull(this double? source) => source == null;
public static bool IsNull(this float? source) => source == null;
public static bool IsNull(this decimal? source) => source == null;
public static bool IsNull(this DateTime? source) => source == null;
public static bool IsNull(this DateTimeOffset? source) => source == null;
public static bool IsNull(this TimeSpan? source) => source == null;
public static bool IsNull(this object? source) => source == null;
#endregion
public static bool IsAlphaNumeric(this string source) => Regex.IsMatch(source, @"^[A-Za-z0-9]+$");
public static bool IsEmail(this string source) => Regex.IsMatch(source, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
public static bool IsPhoneNumber(this string source) => Regex.IsMatch(source, @"^[\d*+#]{1,20}$");
public static bool IsNumeric(this string source) => decimal.TryParse(source, out _);
public static bool IsDate(this string source) => DateTimeOffset.TryParse(source, out _);
public static bool IsTime(this string source) => TimeSpan.TryParse(source, out _);
}