using system;
02.using system.collections;
03.using system.text.regularexpressions;
04.
05.namespace xrinehart.framework.common.validate
06.{
07. /**////
08. /// regularmatch 的摘要说明。
09. ///
10. public class regularmatch
11. {
12. private string _string;
13. private bool _isentirety;
14.
15. /**////
16. /// 可以进行判断的类型
17. ///
18. public enum operation
19. {
20. byte, sbyte, int16, int32, int64, single, double, boolean, char, decimal, datetime, date, time,
21. email, url, chinaphone, chineseword, chinesepostalcode, number, stringmodel_01, stringmodel_02, wideword, narrowword, ipaddress,
22. chinesemobile, chineseid
23. };
24.
25. public regularmatch() { }
26.
27. 用于判断字符串是否是对应类型(默认为包含匹配)#region 用于判断字符串是否是对应类型(默认为包含匹配)
28. public bool isaccordtype(string strverifystring, operation op)
29. {
30. return isaccordtype(strverifystring, op, false);
31. }
32. #endregion
33.
34. 用于判断字符串是否是对应类型(或是否包含对应类型的字符)#region 用于判断字符串是否是对应类型(或是否包含对应类型的字符)
35. /**////
36. /// 用于判断字符串是否是对应类型
37. ///
38. /// string,需要判断的字符串
39. /// operation枚举,用于选择需要进行的操作
40. /// boolean,判断是完全匹配还是包含匹配模式(仅适用于非类型判断时)
41. ///
42. public bool isaccordtype(string strverifystring, operation op, bool isentirety)
43. {
44. _string = strverifystring;
45. _isentirety = isentirety;
46.
47. switch (op)
48. {
49. case operation.byte:
50. {
51. return isbyte();
52. }
53. case operation.sbyte:
54. {
55. return issbyte();
56. }
57. case operation.int16:
58. {
59. return isint16();
60. }
61. case operation.int32:
62. {
63. return isint32();
64. }
65. case operation.int64:
66. {
67. return isint64();
68. }
69. case operation.single:
70. {
71. return issingle();
72. }
73. case operation.double:
74. {
75. return isdouble();
76. }
77. case operation.boolean:
78. {
79. return isboolean();
80. }
81. case operation.char:
82. {
83. return ischar();
84. }
85. case operation.decimal:
86. {
87. return isdecimal();
88. }
89. case operation.datetime:
90. {
91. return isdatetime();
92. }
93. case operation.date:
94. {
95. return isdate();
96. }
97. case operation.time:
98. {
99. return istime();
100. }
101. case operation.ipaddress:
102. {
103. return isipaddress();
104. }
105. case operation.chinaphone:
106. {
107. return ischinaphone();
108. }
109. case operation.chinesepostalcode:
110. {
111. return ischinesepostalcode();
112. }
113. case operation.chinesemobile:
114. {
115. return ischinesemobile();
116. }
117. case operation.email:
118. {
119. return isemail();
120. }
121. case operation.url:
122. {
123. return isurl();
124. }
125. case operation.chineseword:
126. {
127. return ischineseword();
128. }
129. case operation.number:
130. {
131. return isnumber();
132. }
133. case operation.stringmodel_01:
134. {
135. return isstringmodel_01();
136. }
137. case operation.stringmodel_02:
138. {
139. return isstringmodel_02();
140. }
141. case operation.wideword:
142. {
143. return iswideword();
144. }
145. case operation.narrowword:
146. {
147. return isnarrowword();
148. }
149. case operation.chineseid:
150. {
151. return ischineseid();
152. }
153. default:
154. {
155. return false;
156. }
157. }
158. }
159. #endregion
160.
161. 具体验证方法#region 具体验证方法
162.
163. 是否byte类型(8 位的无符号整数): 0 和 255 之间的无符号整数#region 是否byte类型(8 位的无符号整数): 0 和 255 之间的无符号整数
164. /**////
165. /// 是否byte类型(8 位的无符号整数): 0 和 255 之间的无符号整数
166. ///
167. /// boolean
168. protected bool isbyte()
169. {
170. try
171. {
172. byte.parse(_string);
173. }
174. catch
175. {
176. return false;
177. }
178. return true;
179. }
180. #endregion
181.
182. 是否sbyte类型(8 位的有符号整数): -128 到 +127 之间的整数#region 是否sbyte类型(8 位的有符号整数): -128 到 +127 之间的整数
183. /**////
184. /// 是否sbyte类型(8 位的有符号整数): -128 到 +127 之间的整数
185. ///
186. /// boolean
187. protected bool issbyte()
188. {
189. try
190. {
191. sbyte.parse(_string);
192. }
193. catch
194. {
195. return false;
196. }
197. return true;
198. }
199. #endregion
200.
201. 是否int16类型(16 位的有符号整数): -32768 到 +32767 之间的有符号整数#region 是否int16类型(16 位的有符号整数): -32768 到 +32767 之间的有符号整数
202. /**////
203. /// 是否int16类型(16 位的有符号整数): -32768 到 +32767 之间的有符号整数
204. ///
205. /// boolean
206. protected bool isint16()
207. {
208. try
209. {
210. int16.parse(_string);
211. }
212. catch
213. {
214. return false;
215. }
216. return true;
217. }
218. #endregion
219.
220. 是否int32类型(32 位的有符号整数):-2,147,483,648 到 +2,147,483,647 之间的有符号整数#region 是否int32类型(32 位的有符号整数):-2,147,483,648 到 +2,147,483,647 之间的有符号整数
221. /**////
222. /// 是否int32类型(32 位的有符号整数):-2,147,483,648 到 +2,147,483,647 之间的有符号整数
223. ///
224. /// boolean
225. protected bool isint32()
226. {
227. try
228. {
229. int32.parse(_string);
230. }
231. catch
232. {
233. return false;
234. }
235. return true;
236. }
237. #endregion
238.
239. 是否int64类型(64 位的有符号整数): -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807 之间的整数#region 是否int64类型(64 位的有符号整数): -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807 之间的整数
240. /**////
241. /// 是否int64类型(64 位的有符号整数): -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807 之间的整数
242. ///
243. /// boolean
244. protected bool isint64()
245. {
246. try
247. {
248. int64.parse(_string);
249. }
250. catch
251. {
252. return false;
253. }
254. return true;
255. }
256. #endregion
257.
258. 是否single类型(单精度(32 位)浮点数字): -3.402823e38 和 +3.402823e38 之间的单精度 32 位数字#region 是否single类型(单精度(32 位)浮点数字): -3.402823e38 和 +3.402823e38 之间的单精度 32 位数字
259. /**////
260. /// 是否single类型(单精度(32 位)浮点数字): -3.402823e38 和 +3.402823e38 之间的单精度 32 位数字
261. ///
262. /// boolean
263. protected bool issingle()
264. {
265. try
266. {
267. single.parse(_string);
268. }
269. catch
270. {
271. return false;
272. }
273. return true;
274. }
275. #endregion
276.
277. 是否double类型(单精度(64 位)浮点数字): -1.79769313486232e308 和 +1.79769313486232e308 之间的双精度 64 位数字#region 是否double类型(单精度(64 位)浮点数字): -1.79769313486232e308 和 +1.79769313486232e308 之间的双精度 64 位数字
278. /**////
279. /// 是否double类型(单精度(64 位)浮点数字): -1.79769313486232e308 和 +1.79769313486232e308 之间的双精度 64 位数字
280. ///
281. /// boolean
282. protected bool isdouble()
283. {
284. try
285. {
286. double.parse(_string);
287. }
288. catch
289. {
290. return false;
291. }
292. return true;
293. }
294. #endregion
295.
296. 是否boolean类型(布尔值):true 或 false#region 是否boolean类型(布尔值):true 或 false
297. /**////
298. /// 是否double类型(单精度(64 位)浮点数字): -1.79769313486232e308 和 +1.79769313486232e308 之间的双精度 64 位数字
299. ///
300. /// boolean
301. protected bool isboolean()
302. {
303. try
304. {
305. boolean.parse(_string);
306. }
307. catch
308. {
309. return false;
310. }
311. return true;
312. }
313. #endregion
314.
315. 是否char类型(unicode(16 位)字符):该 16 位数字的值范围为从十六进制值 0x0000 到 0xffff#region 是否char类型(unicode(16 位)字符):该 16 位数字的值范围为从十六进制值 0x0000 到 0xffff
316. /**////
317. /// 是否char类型(unicode(16 位)字符):该 16 位数字的值范围为从十六进制值 0x0000 到 0xffff
318. ///
319. /// boolean
320. protected bool ischar()
321. {
322. try
323. {
324. char.parse(_string);
325. }
326. catch
327. {
328. return false;
329. }
330. return true;
331. }
332. #endregion
333.
334. 是否char类型(96 位十进制值):从正 79,228,162,514,264,337,593,543,950,335 到负 79,228,162,514,264,337,593,543,950,335 之间的十进制数#region 是否char类型(96 位十进制值):从正 79,228,162,514,264,337,593,543,950,335 到负 79,228,162,514,264,337,593,543,950,335 之间的十进制数
335. /**////
336. /// 是否char类型(96 位十进制值):从正 79,228,162,514,264,337,593,543,950,335 到负 79,228,162,514,264,337,593,543,950,335 之间的十进制数
337. ///
338. /// boolean
339. protected bool isdecimal()
340. {
341. try
342. {
343. decimal.parse(_string);
344. }
345. catch
346. {
347. return false;
348. }
349. return true;
350. }
351. #endregion
352.
353. 是否datetime类型(表示时间上的一刻): 范围在公元(基督纪元)0001 年 1 月 1 日午夜 12:00:00 到公元 (c.e.) 9999 年 12 月 31 日晚上 11:59:59 之间的日期和时间#region 是否datetime类型(表示时间上的一刻): 范围在公元(基督纪元)0001 年 1 月 1 日午夜 12:00:00 到公元 (c.e.) 9999 年 12 月 31 日晚上 11:59:59 之间的日期和时间
354. /**////
355. /// 是否datetime类型(表示时间上的一刻): 范围在公元(基督纪元)0001 年 1 月 1 日午夜 12:00:00 到公元 (c.e.) 9999 年 12 月 31 日晚上 11:59:59 之间的日期和时间
356. ///
357. /// boolean
358. protected bool isdatetime()
359. {
360. try
361. {
362. datetime.parse(_string);
363. }
364. catch
365. {
366. return false;
367. }
368. return true;
369. }
370. #endregion
371.
372. 是否date类型(表示时间的日期部分): 范围在公元(基督纪元)0001 年 1 月 1 日 到公元 (c.e.) 9999 年 12 月 31 日之间的日期#region 是否date类型(表示时间的日期部分): 范围在公元(基督纪元)0001 年 1 月 1 日 到公元 (c.e.) 9999 年 12 月 31 日之间的日期
373. /**////
374. /// 是否date类型(表示时间的日期部分): 范围在公元(基督纪元)0001 年 1 月 1 日 到公元 (c.e.) 9999 年 12 月 31 日之间的日期
375. ///
376. /// boolean
377. protected bool isdate()
378. {
379. datetime value;
380. try
381. {
382. value = datetime.parse(_string);
383. }
384. catch
385. {
386. return false;
387. }
388. if (value.date.tostring() == _string)
389. {
390. return true;
391. }
392. else
393. {
394. return false;
395. }
396. }
397. #endregion
398.
399. 是否time类型(表示时间部分hhmmss): 范围在夜 12:00:00 到晚上 11:59:59 之间的时间#region 是否time类型(表示时间部分hhmmss): 范围在夜 12:00:00 到晚上 11:59:59 之间的时间
400. /**////
401. /// 是否time类型(表示时间部分hhmmss): 范围在夜 12:00:00 到晚上 11:59:59 之间的时间
402. ///
403. /// boolean
404. protected bool istime()
405. {
406. datetime value;
407. try
408. {
409. value = datetime.parse(_string);
410. }
411. catch
412. {
413. return false;
414. }
415. if (value.year == 1 && value.month == 1 && value.day == 1)
416. {
417. return true;
418. }
419. else
420. {
421. return false;
422. }
423. }
424. #endregion
425.
426. 是否ipaddress类型(ipv4 的情况下使用以点分隔的四部分表示法格式表示,ipv6 的情况下使用冒号与十六进制格式表示)#region 是否ipaddress类型(ipv4 的情况下使用以点分隔的四部分表示法格式表示,ipv6 的情况下使用冒号与十六进制格式表示)
427. /**////
428. /// 是否ipaddress类型(ipv4 的情况下使用以点分隔的四部分表示法格式表示,ipv6 的情况下使用冒号与十六进制格式表示)
429. ///
430. /// boolean
431. protected bool isipaddress()
432. {
433. try
434. {
435. system.net.ipaddress.parse(_string);
436. }
437. catch
438. {
439. return false;
440. }
441. return true;
442. }
443. #endregion
444.
445. 是否中国电话号码类型(xxx/xxxx-xxxxxxx/xxxxxxxx (/d{3,4})-?/d{7,8}):判断是否是(区号:3或4位)-(电话号码:7或8位)#region 是否中国电话号码类型(xxx/xxxx-xxxxxxx/xxxxxxxx (/d{3,4})-?/d{7,8}):判断是否是(区号:3或4位)-(电话号码:7或8位)
446. /**////
447. /// 是否中国电话号码类型(xxx/xxxx-xxxxxxx/xxxxxxxx (/d{3,4})-?/d{7,8}):判断是否是(区号:3或4位)-(电话号码:7或8位)
448. ///
449. /// boolean
450. protected bool ischinaphone()
451. {
452. arraylist aryresult = new arraylist();
453. return commregularmatch(_string, @(/d{3,4})-?/d{7,8}, regexoptions.none, ref aryresult, _isentirety);
454. }
455. #endregion
456.
457. 是否中国邮政编码(6位数字 /d{6})#region 是否中国邮政编码(6位数字 /d{6})
458. /**////
459. /// 是否中国邮政编码(6位数字 /d{6})
460. ///
461. /// boolean
462. protected bool ischinesepostalcode()
463. {
464. arraylist aryresult = new arraylist();
465. return commregularmatch(_string, @/d{6}, regexoptions.none, ref aryresult, _isentirety);
466. }
467. #endregion
468.
469. 是否中国移动电话号码(13开头的总11位数字 13/d{9})#region 是否中国移动电话号码(13开头的总11位数字 13/d{9})
470. /**////
471. /// 是否中国移动电话号码(13开头的总11位数字 13/d{9})
472. ///
473. /// boolean
474. protected bool ischinesemobile()
475. {
476. arraylist aryresult = new arraylist();
477. return commregularmatch(_string, @13/d{9}, regexoptions.none, ref aryresult, _isentirety);
478. }
479. #endregion
480.
481. 是否email类型(xxx@xxx.xxx /w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*)#region 是否email类型(xxx@xxx.xxx /w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*)
482. /**////
483. /// 是否email类型(xxx@xxx.xxx /w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*)
484. ///
485. /// boolean
486. protected bool isemail()
487. {
488. arraylist aryresult = new arraylist();
489. return commregularmatch(_string, @/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*, regexoptions.none, ref aryresult, _isentirety);
490. }
491. #endregion
492.
493. 是否internet url地址类型(http://)#region 是否internet url地址类型(http://)
494. /**////
495. /// 是否internet url地址类型(http://)
496. ///
497. /// boolean
498. protected bool isurl()
499. {
500. arraylist aryresult = new arraylist();
501. return commregularmatch(_string, @http://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?, regexoptions.none, ref aryresult, _isentirety);
502. }
503. #endregion
504.
505. 是否中文字符([/u4e00-/u9fa5])#region 是否中文字符([/u4e00-/u9fa5])
506. /**////
507. /// 是否中文字符([/u4e00-/u9fa5])
508. ///
509. /// boolean
510. protected bool ischineseword()
511. {
512. arraylist aryresult = new arraylist();
513. return commregularmatch(_string, @[/u4e00-/u9fa5], regexoptions.none, ref aryresult, _isentirety);
514. }
515. #endregion
516.
517. 是否是数字(0到9的数字[/d]+):不包括符号.和-#region 是否是数字(0到9的数字[/d]+):不包括符号.和-
518. /**////
519. /// 是否是数字(0到9的数字[/d]+):不包括符号.和-
520. ///
521. /// boolean
522. protected bool isnumber()
523. {
524. arraylist aryresult = new arraylist();
525. return commregularmatch(_string, @[/d]+, regexoptions.none, ref aryresult, _isentirety);
526. }
527. #endregion
528.
529. 是否只包含数字,英文和下划线([/w]+)#region 是否只包含数字,英文和下划线([/w]+)
530. /**////
531. /// 是否只包含数字,英文和下划线([/w]+)
532. ///
533. /// boolean
534. protected bool isstringmodel_01()
535. {
536. arraylist aryresult = new arraylist();
537. return commregularmatch(_string, @[/w]+, regexoptions.none, ref aryresult, _isentirety);
538. }
539. #endregion
540.
541. 是否大写首字母的英文字母([a-z][a-z]+)#region 是否大写首字母的英文字母([a-z][a-z]+)
542. /**////
543. /// 是否大写首字母的英文字母([a-z][a-z]+)
544. ///
545. /// boolean
546. protected bool isstringmodel_02()
547. {
548. arraylist aryresult = new arraylist();
549. return commregularmatch(_string, @[a-z][a-z]+, regexoptions.none, ref aryresult, _isentirety);
550. }
551. #endregion
552.
553. 是否全角字符([^/x00-/xff]):包括汉字在内#region 是否全角字符([^/x00-/xff]):包括汉字在内
554. /**////
555. /// 是否全角字符([^/x00-/xff]):包括汉字在内
556. ///
557. /// boolean
558. protected bool iswideword()
559. {
560. arraylist aryresult = new arraylist();
561. return commregularmatch(_string, @[^/x00-/xff], regexoptions.none, ref aryresult, _isentirety);
562. }
563. #endregion
564.
565. 是否半角字符([/x00-/xff])#region 是否半角字符([/x00-/xff])
566. /**////
567. /// 是否半角字符([^/x00-/xff]):包括汉字在内
568. ///
569. /// boolean
570. protected bool isnarrowword()
571. {
572. arraylist aryresult = new arraylist();
573. return commregularmatch(_string, @[/x00-/xff], regexoptions.none, ref aryresult, _isentirety);
574. }
575. #endregion
576.
577. 是否合法的中国身份证号码#region 是否合法的中国身份证号码
578. protected bool ischineseid()
579. {
580. if (_string.length == 15)
581. {
582. _string = cidupdate(_string);
583. }
584. if (_string.length == 18)
585. {
586. string strresult = checkcidinfo(_string);
587. if (strresult == 非法地区 || strresult == 非法生日 || strresult == 非法证号)
588. {
589. return false;
590. }
591. else
592. {
593. return true;
594. }
595. }
596. else
597. {
598. return false;
599. }
600. }
601. #endregion
602.
603. #endregion
604.
605. 通用正则表达式判断函数#region 通用正则表达式判断函数
606. /**////
607. /// 通用正则表达式判断函数
608. ///
609. /// string,用于匹配的字符串
610. /// string,正则表达式
611. /// regexoptions,配置正则表达式的选项
612. /// arraylist,分解的字符串内容
613. /// boolean,是否需要完全匹配
614. ///
615. public bool commregularmatch(string strverifystring, string strregular, system.text.regularexpressions.regexoptions regoption, ref system.collections.arraylist aryresult, bool isentirety)
616. {
617. system.text.regularexpressions.regex r;
618. system.text.regularexpressions.match m;
619.
620. 如果需要完全匹配的处理#region 如果需要完全匹配的处理
621. if (isentirety)
622. {
623. strregular = strregular.insert(0, @/a);
624. strregular = strregular.insert(strregular.length, @/z);
625. }
626. #endregion
627.
628. try
629. {
630. r = new system.text.regularexpressions.regex(strregular, regoption);
631. }
632. catch (system.exception e)
633. {
634. throw (e);
635. }
636.
637. for (m = r.match(strverifystring); m.success; m = m.nextmatch())
638. {
639. aryresult.add(m);
640. }
641.
642. if (aryresult.count == 0)
643. {
644. return false;
645. }
646. else
647. {
648. return true;
649. }
650. }
651. #endregion
652.
653. 中国身份证号码验证#region 中国身份证号码验证
654. private string checkcidinfo(string cid)
655. {
656. string[] acity = new string[] { null, null, null, null, null, null, null, null, null, null, null, 北京, 天津, 河北, 山西, 内蒙古, null, null, null, null, null, 辽宁, 吉林, 黑龙江, null, null, null, null, null, null, null, 上海, 江苏, 浙江, 安微, 福建, 江西, 山东, null, null, null, 河南, 湖北, 湖南, 广东, 广西, 海南, null, null, null, 重庆, 四川, 贵州, 云南, 西藏, null, null, null, null, null, null, 陕西, 甘肃, 青海, 宁夏, 新疆, null, null, null, null, null, 台湾, null, null, null, null, null, null, null, null, null, 香港, 澳门, null, null, null, null, null, null, null, null, 国外 };
657. double isum = 0;
658. string info = string.empty;
659. system.text.regularexpressions.regex rg = new system.text.regularexpressions.regex(@^/d{17}(/d|x)$);
660. system.text.regularexpressions.match mc = rg.match(cid);
661. if (!mc.success)
662. {
663. return string.empty;
664. }
665. cid = cid.tolower();
666. cid = cid.replace(x, a);
667. if (acity[int.parse(cid.substring(0, 2))] == null)
668. {
669. return 非法地区;
670. }
671. try
672. {
673. datetime.parse(cid.substring(6, 4) + - + cid.substring(10, 2) + - + cid.substring(12, 2));
674. }
675. catch
676. {
677. return 非法生日;
678. }
679. for (int i = 17; i >= 0; i--)
680. {
681. isum += (system.math.pow(2, i) % 11) * int.parse(cid[17 - i].tostring(), system.globalization.numberstyles.hexnumber);
682. }
683. if (isum % 11 != 1)
684. {
685. return (非法证号);
686. }
687. else
688. {
689. return (acity[int.parse(cid.substring(0, 2))] + , + cid.substring(6, 4) + - + cid.substring(10, 2) + - + cid.substring(12, 2) + , + (int.parse(cid.substring(16, 1)) % 2 == 1 ? 男 : 女));
690. }
691. }
692. #endregion
693.
694. 身份证号码15升级为18位#region 身份证号码15升级为18位
695. private string cidupdate(string shortcid)
696. {
697. char[] strjiaoyan = { '1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2' };
698. int[] intquan = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
699. string strtemp;
700. int inttemp = 0;
701.
702. strtemp = shortcid.substring(0, 6) + 19 + shortcid.substring(6);
703. for (int i = 0; i 704. {
705. inttemp += int.parse(strtemp.substring(i, 1)) * intquan[i];
706. }
707. inttemp = inttemp % 11;
708. return strtemp + strjiaoyan[inttemp];
709. }
710. #endregion
711. }
712.}
http://www.bkjia.com/phpjc/953965.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/953965.htmltecharticle一个常用的正则表达验证类 很早以前写的一个正则表达式验证工具类,包含了一些常见的校验和支持自定义的正则表达式匹配,可以选择完...