/
PhoneAttribute.cs
126 lines (110 loc) · 4.23 KB
/
PhoneAttribute.cs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace System.ComponentModel.DataAnnotations
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,
AllowMultiple = false)]
public sealed class PhoneAttribute : DataTypeAttribute
{
private const string AdditionalPhoneNumberCharacters = "-.()";
private const string ExtensionAbbreviationExtDot = "ext.";
private const string ExtensionAbbreviationExt = "ext";
private const string ExtensionAbbreviationX = "x";
public PhoneAttribute()
: base(DataType.PhoneNumber)
{
// Set DefaultErrorMessage not ErrorMessage, allowing user to set
// ErrorMessageResourceType and ErrorMessageResourceName to use localized messages.
DefaultErrorMessage = SR.PhoneAttribute_Invalid;
}
public override bool IsValid(object? value)
{
if (value == null)
{
return true;
}
if (!(value is string valueAsString))
{
return false;
}
ReadOnlySpan<char> valueSpan = valueAsString.Replace("+", string.Empty).AsSpan().TrimEnd();
valueSpan = RemoveExtension(valueSpan);
bool digitFound = false;
foreach (char c in valueSpan)
{
if (char.IsDigit(c))
{
digitFound = true;
break;
}
}
if (!digitFound)
{
return false;
}
foreach (char c in valueSpan)
{
if (!(char.IsDigit(c)
|| char.IsWhiteSpace(c)
|| AdditionalPhoneNumberCharacters.Contains(c)))
{
return false;
}
}
return true;
}
private static ReadOnlySpan<char> RemoveExtension(ReadOnlySpan<char> potentialPhoneNumber)
{
int lastIndexOfExtension = potentialPhoneNumber
.LastIndexOf(ExtensionAbbreviationExtDot, StringComparison.OrdinalIgnoreCase);
if (lastIndexOfExtension >= 0)
{
ReadOnlySpan<char> extension = potentialPhoneNumber.Slice(
lastIndexOfExtension + ExtensionAbbreviationExtDot.Length);
if (MatchesExtension(extension))
{
return potentialPhoneNumber.Slice(0, lastIndexOfExtension);
}
}
lastIndexOfExtension = potentialPhoneNumber
.LastIndexOf(ExtensionAbbreviationExt, StringComparison.OrdinalIgnoreCase);
if (lastIndexOfExtension >= 0)
{
ReadOnlySpan<char> extension = potentialPhoneNumber.Slice(
lastIndexOfExtension + ExtensionAbbreviationExt.Length);
if (MatchesExtension(extension))
{
return potentialPhoneNumber.Slice(0, lastIndexOfExtension);
}
}
lastIndexOfExtension = potentialPhoneNumber
.LastIndexOf(ExtensionAbbreviationX, StringComparison.OrdinalIgnoreCase);
if (lastIndexOfExtension >= 0)
{
ReadOnlySpan<char> extension = potentialPhoneNumber.Slice(
lastIndexOfExtension + ExtensionAbbreviationX.Length);
if (MatchesExtension(extension))
{
return potentialPhoneNumber.Slice(0, lastIndexOfExtension);
}
}
return potentialPhoneNumber;
}
private static bool MatchesExtension(ReadOnlySpan<char> potentialExtension)
{
potentialExtension = potentialExtension.TrimStart();
if (potentialExtension.Length == 0)
{
return false;
}
foreach (char c in potentialExtension)
{
if (!char.IsDigit(c))
{
return false;
}
}
return true;
}
}
}