验证是不是合法的18位身份证号码代码:
1 public static bool IsValidIDNumber(string strId) 2 { 3 Regex regId = new Regex(@"^\d{17}(\d|x)$", RegexOptions.IgnoreCase); 4 if (strId.Length != 18) 5 { 6 return false; 7 } 8 else 9 { 10 if (!regId.IsMatch(strId)) 11 { 12 return false; 13 } 14 else 15 { 16 int iS = 0; 17 int[] iW = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; 18 string LastCode = "10X98765432"; 19 for (int i = 0; i < 17; i++) 20 { 21 iS += int.Parse(strId.Substring(i, 1)) * iW[i]; 22 } 23 int iY = iS % 11; 24 if (strId.Substring(17, 1) != LastCode.Substring(iY, 1)) 25 return false; 26 return true; 27 } 28 } 29 }