摘要:c# 認証身分證
protected bool IdCheck(string strUserID) { int intAreaNo=0; //區域碼變數。 int intCheckSum=0;//檢核碼變數。 int intCount=0;//計數變數。 string strAreaCode;//區域碼變數。 //轉換為大寫。 strUserID = strUserID.ToString().ToUpper(); //取得首碼字母。 strAreaCode = strUserID.Substring(0,1); //設定起始值。 bool check = false; //確定身份證有10碼。 if(strUserID.Length == 10) { //確定首碼在A-Z之間。 if (IsNatural_Number(strAreaCode)) { //確定第二碼是數字 1 或 2。(1為男生, 2為女生) if (strUserID.Substring(1, 1) == "1" || strUserID.Substring(1, 1) == "2") { //取得英文字母對應編號。A -> 10, B -> 11 等等。 string abc = "ABCDEFGHJKLMNPQRSTUVXYWZIO"; for (int i = 0; i < abc.Length; i++) { if (strAreaCode == abc.Substring(i, 1)) { intAreaNo = i + 10; } } strUserID = intAreaNo.ToString() + strUserID.Substring(1, 9); int count=0; for (int j = 10; j >= 0; j--) { if (j == 0) { count += Convert.ToInt32(strUserID.ToString().Substring(10, 1)) * 1; } else { int a = strUserID.Length - j - 1; count += Convert.ToInt32(j.ToString().Substring(0, 1)) * Convert.ToInt32(strUserID.Substring(a,1)); } } if ((count * 1.0) % 10 == 0) { check = true; } } else { } } else { } } else { } return check; }
//判斷是否為英文字母 public bool IsNatural_English(string str) { System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[A-Za-z]+$"); return reg1.IsMatch(str); }