C#驗證身份證號
Code
private bool CheckIDCard(string Id)
{
if (Id.Length == 18)
{
bool check = CheckIDCard18(Id);
return check;
}
else if (Id.Length == 15)
{
bool check = CheckIDCard15(Id);
return check;
}
else
{
return false;
}
}
private bool CheckIDCard18(string Id)
{
long n = 0;
if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
{
return false;//數字驗證
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2)) == -1)
{
return false;//省份驗證
}
string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日驗證
}
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] Ai = Id.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += int.Parse(Wi) * int.Parse(Ai.ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
{
return false;//校驗碼驗證
}
return true;//符合GB11643-1999標準
}
private bool CheckIDCard15(string Id)
{
long n = 0;
if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
{
return false;//數字驗證
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2)) == -1)
{
return false;//省份驗證
}
string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日驗證
}
return true;//符合15位身份證標準
}
private bool CheckIDCard(string Id)
{
if (Id.Length == 18)
{
bool check = CheckIDCard18(Id);
return check;
}
else if (Id.Length == 15)
{
bool check = CheckIDCard15(Id);
return check;
}
else
{
return false;
}
}
private bool CheckIDCard18(string Id)
{
long n = 0;
if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
{
return false;//數字驗證
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2)) == -1)
{
return false;//省份驗證
}
string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日驗證
}
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] Ai = Id.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += int.Parse(Wi) * int.Parse(Ai.ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
{
return false;//校驗碼驗證
}
return true;//符合GB11643-1999標準
}
private bool CheckIDCard15(string Id)
{
long n = 0;
if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
{
return false;//數字驗證
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2)) == -1)
{
return false;//省份驗證
}
string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日驗證
}
return true;//符合15位身份證標準
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-558060/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PHP 驗證身份證號碼PHP
- 中國身份證號驗證庫
- C++身份證號驗證C++
- js正則驗證身份證號JS
- 身份證號碼驗證系統
- 身份證號碼之js驗證JS
- 身份證號碼驗證演算法演算法
- js實現身份證號碼驗證JS
- C# Web Service 身份驗證C#Web
- 精確驗證身份證號碼程式碼
- javascript身份證號碼校驗JavaScript
- Javascript使用正則驗證身份證號(簡單)JavaScript
- javascript 驗證身份證JavaScript
- JS驗證18位身份證號的正確性JS
- javascript實現的身份證號碼驗證程式碼JavaScript
- 身份證驗證工具類
- java 實現從15位~18位的身份證號碼轉換,校驗中國大陸公民身份證、香港居民身份證、澳門身份證和臺灣身份證。Java
- WEB身份驗證Web
- swift 郵箱、密碼、手機號、身份證驗證正則Swift密碼
- JS校驗身份證號的合法性JS
- PHP 身份證精確匹配驗證PHP
- 正則實現二代身份證號碼驗證詳解
- javascript實現的身份證號碼合法性驗證程式碼JavaScript
- 精確驗證身份證號碼合法性程式碼例項
- Oracle的身份驗證Oracle
- C++身份核驗介面程式碼、身份證OCR、身份證實名認證APIC++API
- php與js方式驗證手機號碼和郵件地址的合法性,js驗證身份證號碼PHPJS
- PHP校驗15位和18位身份證號PHP
- 身份證號碼校驗位的計算方式
- iOS身份證號識別iOS
- JS驗證身份證的合法性JS
- jQuery正則驗證15/18身份證jQuery
- 用 python 製作全國身份證號驗證及查詢系統Python
- jQuery如何用正規表示式驗證手機號、身份證號、中文名稱jQuery
- 作業系統身份驗證和口令檔案身份驗證總結作業系統
- oracle常見身份驗證Oracle
- 身份證號碼的正規表示式及驗證詳解(JavaScript,Regex)JavaScript
- javascript 驗證身份證完全版,根據身份證獲取性別年齡JavaScript