c#驗證字符串是否是身份證、email、正整數的方法


 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 using System.Text.RegularExpressions;
 7 
 8 namespace Logistics
 9 {
10     /// <summary>
11     /// 通用驗證類
12     /// </summary>
13     class DataValidate
14     {
15         /// <summary>
16         /// 驗證正整數
17         /// </summary>
18         /// <param name="txt"></param>
19         /// <returns></returns>
20         public static bool IsInteger(string txt)
21         {
22             Regex objReg = new Regex(@"^[1-9]\d*$");
23             return objReg.IsMatch(txt);
24         }
25         /// <summary>
26         /// 驗證是否是Email
27         /// </summary>
28         /// <param name="txt"></param>
29         /// <returns></returns>
30         public static bool IsEmail(string txt)
31         {
32             Regex objReg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
33             return objReg.IsMatch(txt);
34         }
35         /// <summary>
36         /// 驗證身份證
37         /// </summary>
38         /// <param name="txt"></param>
39         /// <returns></returns>
40         public static bool IsIdentityCard(string txt)
41         {
42             Regex objReg = new Regex(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
43             return objReg.IsMatch(txt);
44         }
45     }
46 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM