forked from wangyx0055/DisklessSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextInputStringValidator.cs
266 lines (248 loc) · 9.59 KB
/
TextInputStringValidator.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace NoDiskSystem
{
class TextInputStringValidator
{
#region 验证输入字符串为数字
/// <summary>
/// 验证输入字符串为数字
/// </summary>
/// <param name="strln">输入字符</param>
/// <returns>返回一个bool类型的值</returns>
public static bool IsNumber(string strln)
{
return Regex.IsMatch(strln, "^([0]|([1-9]+\\d{0,}?))(.[\\d]+)?$");
}
#endregion
/// <summary>
/// 验证输入字符串为英文字母数字组成的字符串
/// </summary>
/// <param name="strln">输入字符</param>
/// <returns>返回一个bool类型的值</returns>
public static bool IsEnglishWithNum(string strln) {
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^[A-Za-z0-9_]+$");
if (!reg.IsMatch(strln))
{
return false;
}
else {
return true;
}
}
/// <summary>
/// 判断用户输入是否为日期
/// </summary>
/// <param name="strln"></param>
/// <returns></returns>
/// <remarks>
/// 可判断格式如下(其中-可替换为/,不影响验证)
/// YYYY | YYYY-MM | YYYY-MM-DD | YYYY-MM-DD HH:MM:SS | YYYY-MM-DD HH:MM:SS.FFF
/// </remarks>
public static bool IsDateTime(string strln)
{
if (null == strln)
{
return false;
}
string regexDate = @"[1-2]{1}[0-9]{3}((-|\/|\.){1}(([0]?[1-9]{1})|(1[0-2]{1}))((-|\/|\.){1}((([0]?[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1})))( (([0-1]{1}[0-9]{1})|2[0-3]{1}):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})(\.[0-9]{3})?)?)?)?$";
if (Regex.IsMatch(strln, regexDate))
{
//以下各月份日期验证,保证验证的完整性
int _IndexY = -1;
int _IndexM = -1;
int _IndexD = -1;
if (-1 != (_IndexY = strln.IndexOf("-")))
{
_IndexM = strln.IndexOf("-", _IndexY + 1);
_IndexD = strln.IndexOf(":");
}
else
{
_IndexY = strln.IndexOf("/");
_IndexM = strln.IndexOf("/", _IndexY + 1);
_IndexD = strln.IndexOf(":");
}
//不包含日期部分,直接返回true
if (-1 == _IndexM)
return true;
if (-1 == _IndexD)
{
_IndexD = strln.Length + 3;
}
int iYear = Convert.ToInt32(strln.Substring(0, _IndexY));
int iMonth = Convert.ToInt32(strln.Substring(_IndexY + 1, _IndexM - _IndexY - 1));
int iDate = Convert.ToInt32(strln.Substring(_IndexM + 1, _IndexD - _IndexM - 4));
//判断月份日期
if ((iMonth < 8 && 1 == iMonth % 2) || (iMonth > 8 && 0 == iMonth % 2))
{
if (iDate < 32)
return true;
}
else
{
if (iMonth != 2)
{
if (iDate < 31)
return true;
}
else
{
//闰年
if ((0 == iYear % 400) || (0 == iYear % 4 && 0 < iYear % 100))
{
if (iDate < 30)
return true;
}
else
{
if (iDate < 29)
return true;
}
}
}
}
return false;
}
#region 验证手机号
/// <summary>
/// 验证输入字符串为18位的手机号码
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsMobile(string strln)
{
return Regex.IsMatch(strln, @"^1[0123456789]\d{9}$", RegexOptions.IgnoreCase);
}
#endregion
/// <summary>
/// 验证身份证是否有效
/// </summary>
/// <param name="strln"></param>
/// <returns></returns>
public static bool IsIDCard(string strln)
{
if (strln.Length == 18)
{
bool check = IsIDCard18(strln);
return check;
}
else if (strln.Length == 15)
{
bool check = IsIDCard15(strln);
return check;
}
else
{
return false;
}
}
/// <summary>
/// 验证输入字符串为18位的身份证号码
/// </summary>
/// <param name="strln">输入的字符</param>
/// <returns></returns>
public static bool IsIDCard18(string strln)
{
long n = 0;
if (long.TryParse(strln.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(strln.Replace('x', '0').Replace('X', '0'), out n) == false)
{
return false;//数字验证
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(strln.Remove(2)) == -1)
{
return false;//省份验证
}
string birth = strln.Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日验证
}
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] Ai = strln.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] != strln.Substring(17, 1).ToLower())
{
return false;//校验码验证
}
return true;//符合GB11643-1999标准
}
/// <summary>
/// 验证输入字符串为15位的身份证号码
/// </summary>
/// <param name="strln">输入的字符</param>
/// <returns></returns>
public static bool IsIDCard15(string strln)
{
long n = 0;
if (long.TryParse(strln, out n) == false || n < Math.Pow(10, 14))
{
return false;//数字验证
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(strln.Remove(2)) == -1)
{
return false;//省份验证
}
string birth = strln.Substring(6, 6).Insert(4, "-").Insert(2, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日验证
}
return true;//符合15位身份证标准
}
/// <summary>
/// 验证输入字符串为电话号码
/// </summary>
/// <param name="P_str_phone">输入字符串</param>
/// <returns>返回一个bool类型的值</returns>
public static bool IsPhone(string strln)
{
return Regex.IsMatch(strln, @"(^(\d{2,4}[-_-—]?)?\d{3,8}([-_-—]?\d{3,8})?([-_-—]?\d{1,7})?$)|(^0?1[35]\d{9}$)");
//弱一点的验证: @"\d{3,4}-\d{7,8}"
}
/// <summary>
/// 验证是否是有效邮箱地址
/// </summary>
/// <param name="strln">输入的字符</param>
/// <returns></returns>
public static bool IsEmail(string strln)
{
return Regex.IsMatch(strln, @"^([\w-\.]+)@((
[0−9]1,3\.[0−9]1,3\.[0−9]1,3\.)|(([\w−]+\.)+))([a−zA−Z]2,4|[0−9]1,3)(
?)$");
}
/// <summary>
/// 验证是否是有效传真号码
/// </summary>
/// <param name="strln">输入的字符</param>
/// <returns></returns>
public static bool IsFax(string strln)
{
return Regex.IsMatch(strln, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
}
/// <summary>
/// 验证是否只含有汉字
/// </summary>
/// <param name="strln">输入的字符</param>
/// <returns></returns>
public static bool IsOnllyChinese(string strln)
{
return Regex.IsMatch(strln, @"^[\u4e00-\u9fa5]+$");
}
}
}