forked from bdukes/sugar
-
Notifications
You must be signed in to change notification settings - Fork 6
/
StringValidations.cs
200 lines (183 loc) · 7.62 KB
/
StringValidations.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
/// <summary>
/// Summary for the Validation class
/// </summary>
public static class Validation {
/// <summary>
/// Determines whether the specified eval string contains only alpha characters.
/// </summary>
/// <param name="evalString">The eval string.</param>
/// <returns>
/// <c>true</c> if the specified eval string is alpha; otherwise, <c>false</c>.
/// </returns>
public static bool IsAlpha(this string evalString) {
return !Regex.IsMatch(evalString, RegexPattern.ALPHA);
}
/// <summary>
/// Determines whether the specified eval string contains only alphanumeric characters
/// </summary>
/// <param name="evalString">The eval string.</param>
/// <returns>
/// <c>true</c> if the string is alphanumeric; otherwise, <c>false</c>.
/// </returns>
public static bool IsAlphaNumeric(this string evalString) {
return !Regex.IsMatch(evalString, RegexPattern.ALPHA_NUMERIC);
}
/// <summary>
/// Determines whether the specified eval string contains only alphanumeric characters
/// </summary>
/// <param name="evalString">The eval string.</param>
/// <param name="allowSpaces">if set to <c>true</c> [allow spaces].</param>
/// <returns>
/// <c>true</c> if the string is alphanumeric; otherwise, <c>false</c>.
/// </returns>
public static bool IsAlphaNumeric(this string evalString, bool allowSpaces) {
if (allowSpaces)
return !Regex.IsMatch(evalString, RegexPattern.ALPHA_NUMERIC_SPACE);
return IsAlphaNumeric(evalString);
}
/// <summary>
/// Determines whether the specified eval string contains only numeric characters
/// </summary>
/// <param name="evalString">The eval string.</param>
/// <returns>
/// <c>true</c> if the string is numeric; otherwise, <c>false</c>.
/// </returns>
public static bool IsNumeric(this string evalString) {
return !Regex.IsMatch(evalString, RegexPattern.NUMERIC);
}
/// <summary>
/// Determines whether the specified email address string is valid based on regular expression evaluation.
/// </summary>
/// <param name="emailAddressString">The email address string.</param>
/// <returns>
/// <c>true</c> if the specified email address is valid; otherwise, <c>false</c>.
/// </returns>
public static bool IsEmail(this string emailAddressString) {
return Regex.IsMatch(emailAddressString, RegexPattern.EMAIL);
}
/// <summary>
/// Determines whether the specified string is lower case.
/// </summary>
/// <param name="inputString">The input string.</param>
/// <returns>
/// <c>true</c> if the specified string is lower case; otherwise, <c>false</c>.
/// </returns>
public static bool IsLowerCase(this string inputString) {
return Regex.IsMatch(inputString, RegexPattern.LOWER_CASE);
}
/// <summary>
/// Determines whether the specified string is upper case.
/// </summary>
/// <param name="inputString">The input string.</param>
/// <returns>
/// <c>true</c> if the specified string is upper case; otherwise, <c>false</c>.
/// </returns>
public static bool IsUpperCase(this string inputString) {
return Regex.IsMatch(inputString, RegexPattern.UPPER_CASE);
}
/// <summary>
/// Determines whether the specified string is a valid GUID.
/// </summary>
/// <param name="guid">The GUID.</param>
/// <returns>
/// <c>true</c> if the specified string is a valid GUID; otherwise, <c>false</c>.
/// </returns>
public static bool IsGuid(this string guid) {
return Regex.IsMatch(guid, RegexPattern.GUID);
}
/// <summary>
/// Determines whether the specified string is a valid US Zip Code, using either 5 or 5+4 format.
/// </summary>
/// <param name="zipCode">The zip code.</param>
/// <returns>
/// <c>true</c> if it is a valid zip code; otherwise, <c>false</c>.
/// </returns>
public static bool IsZIPCodeAny(this string zipCode) {
return Regex.IsMatch(zipCode, RegexPattern.US_ZIPCODE_PLUS_FOUR_OPTIONAL);
}
/// <summary>
/// Determines whether the specified string is a valid US Zip Code, using the 5 digit format.
/// </summary>
/// <param name="zipCode">The zip code.</param>
/// <returns>
/// <c>true</c> if it is a valid zip code; otherwise, <c>false</c>.
/// </returns>
public static bool IsZIPCodeFive(this string zipCode) {
return Regex.IsMatch(zipCode, RegexPattern.US_ZIPCODE);
}
/// <summary>
/// Determines whether the specified string is a valid US Zip Code, using the 5+4 format.
/// </summary>
/// <param name="zipCode">The zip code.</param>
/// <returns>
/// <c>true</c> if it is a valid zip code; otherwise, <c>false</c>.
/// </returns>
public static bool IsZIPCodeFivePlusFour(this string zipCode) {
return Regex.IsMatch(zipCode, RegexPattern.US_ZIPCODE_PLUS_FOUR);
}
/// <summary>
/// Determines whether the specified string is a valid Social Security number. Dashes are optional.
/// </summary>
/// <param name="socialSecurityNumber">The Social Security Number</param>
/// <returns>
/// <c>true</c> if it is a valid Social Security number; otherwise, <c>false</c>.
/// </returns>
public static bool IsSocialSecurityNumber(this string socialSecurityNumber) {
return Regex.IsMatch(socialSecurityNumber, RegexPattern.SOCIAL_SECURITY);
}
/// <summary>
/// Determines whether the specified string is a valid IP address.
/// </summary>
/// <param name="ipAddress">The ip address.</param>
/// <returns>
/// <c>true</c> if valid; otherwise, <c>false</c>.
/// </returns>
public static bool IsIPAddress(this string ipAddress) {
return Regex.IsMatch(ipAddress, RegexPattern.IP_ADDRESS);
}
/// <summary>
/// Determines whether the specified string is a valid US phone number using the referenced regex string.
/// </summary>
/// <param name="telephoneNumber">The telephone number.</param>
/// <returns>
/// <c>true</c> if valid; otherwise, <c>false</c>.
/// </returns>
public static bool IsUSTelephoneNumber(this string telephoneNumber) {
return Regex.IsMatch(telephoneNumber, RegexPattern.US_TELEPHONE);
}
/// <summary>
/// Determines whether the specified string is a valid currency string using the referenced regex string.
/// </summary>
/// <param name="currency">The currency string.</param>
/// <returns>
/// <c>true</c> if valid; otherwise, <c>false</c>.
/// </returns>
public static bool IsUSCurrency(this string currency) {
return Regex.IsMatch(currency, RegexPattern.US_CURRENCY);
}
/// <summary>
/// Determines whether the specified string is a valid URL string using the referenced regex string.
/// </summary>
/// <param name="url">The URL string.</param>
/// <returns>
/// <c>true</c> if valid; otherwise, <c>false</c>.
/// </returns>
public static bool IsURL(this string url) {
return Regex.IsMatch(url, RegexPattern.URL);
}
/// <summary>
/// Determines whether the specified string is consider a strong password based on the supplied string.
/// </summary>
/// <param name="password">The password.</param>
/// <returns>
/// <c>true</c> if strong; otherwise, <c>false</c>.
/// </returns>
public static bool IsStrongPassword(this string password) {
return Regex.IsMatch(password, RegexPattern.STRONG_PASSWORD);
}
}