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 }