C#实现的18位身份证号码最新算法[验证部分]

系统 1967 0

private void btnVerification_Click(object sender, EventArgs e)
{
Regex reg = new Regex(@"^\d{17}(\d|X)");
string sTemp=tbVerification.Text;
lbCardInfo.Items.Clear();
if (sTemp == string.Empty)
{
MessageBox.Show("没有输入任何身份证号码","请注意",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
if (sTemp.Length != 18)
{
MessageBox.Show("输入身份证号码的长度应为18位", "请注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (reg.Matches(sTemp).Count==0)
{
MessageBox.Show("输入身份证号码的格式有误", "请注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

if (!isDiscValid(sTemp.Substring(0,6)))
{
MessageBox.Show("输入的身份证号码行政区划代码无效", "请注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

if (!isBirthValid(sTemp.Substring(6, 8)))
{
MessageBox.Show("输入的身份证号码出生日期无效", "请注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else
lbCardInfo.Items.Add(string.Format("出生日期:{0}年{1}月{2}日", sTemp.Substring(6, 4), sTemp.Substring(10, 2), sTemp.Substring(12, 2)));

if (!isSexValid(sTemp.Substring(14, 3)))
{
MessageBox.Show("输入的身份证号码性别顺序码无效", "请注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else
{
lbCardInfo.Items.Add("性别:"+((Convert.ToInt32(sTemp.Substring(14, 3)) % 2==0) ? "女" : "男"));
}

if (!isParityValid(sTemp))
{
MessageBox.Show("输入的身份证号码验证码无效", "请注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
lbCardInfo.Items.Insert(0, "身份证号:" + sTemp);
lbCardInfo.Items.Insert(0, "验证解读信息:");
lbCardInfo.Items.Add("完成验证!");
gbCardInfo.Visible = true;
}

#region
private void GetDiscInfo(string sFilenName)
{
if (File.Exists(sFilenName))
{
using (StreamReader sr = new StreamReader(Environment.CurrentDirectory + @"\" + sFilenName, Encoding.Default))
{
string sLine = string.Empty;
Regex re = new Regex(@"[\s]{1,}", RegexOptions.Compiled);
while (!sr.EndOfStream)
{
sLine = sr.ReadLine();
sLine = re.Replace(sLine, " ");
string[] sTemp = sLine.Split(new char[] { ' ' });

if (sTemp[0].EndsWith("0000"))
{
provinceCode.Add(new clsAdministrativeDivisionsCode(sTemp[0], sTemp[1]));
}
else
{
if (sTemp[0].EndsWith("00"))
cityCode.Add(new clsAdministrativeDivisionsCode(sTemp[0], sTemp[1]));
else
if (sTemp[1].EndsWith("市辖区"))
cityCode.Add(new clsAdministrativeDivisionsCode(sTemp[0], " " + sTemp[1]));
else
cityCode.Add(new clsAdministrativeDivisionsCode(sTemp[0], " " + sTemp[1]));
}
}
}
}
else
{
MessageBox.Show("文件{0}不存在!", "请注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}

private List<clsAdministrativeDivisionsCode> GetSomeCodes(string sCondition)
{
List<clsAdministrativeDivisionsCode> tempCity = new List<clsAdministrativeDivisionsCode>();
foreach (clsAdministrativeDivisionsCode temp in cityCode)
if (sCondition == temp.AdCode.Substring(0, 2))
tempCity.Add(temp);
return tempCity;
}

private string GenParityBit(string s17)
{
int[] Weight = new int[] { 7, 9 ,10, 5 ,8, 4, 2 ,1, 6 ,3, 7, 9, 10, 5, 8 ,4, 2};
string Parity = "10X98765432";
int s = 0;
for (int i = 0; i < s17.Length; i++)
{
s += Int32.Parse(s17[i].ToString())*Weight[i];
}
return Parity[s % 11].ToString();
}

//iSex=0 男
private List<string> GenRnd(int iSex)
{
Random rd=new Random();
List<string> sTemp = new List<string>();
int i=0;
while(i<maxNum)
{
int rndNum = rd.Next(0,1000);
if(rndNum % 2 == iSex)rndNum++;
if (rndNum >= 1000) continue;
string s3=rndNum.ToString().PadLeft(3,'0');
if(!sTemp.Contains(s3))
{
sTemp.Add(s3);
i++;
}
}
return sTemp;
}

private List<string> GetID()
{
List<string> tempID = new List<string>();
List<string> sexRndNum = GenRnd(rbMan.Checked?0:1);
string str18 = string.Empty;
for (int i = 0; i < maxNum; i++)
{
string str17=string.Empty;
string sCity = cbCities.SelectedValue.ToString();
string sYMD=dtpBirth.Value.Year.ToString() + dtpBirth.Value.Month.ToString().PadLeft(2, '0') + dtpBirth.Value.Day.ToString().PadLeft(2, '0');
str17=sCity+sYMD+sexRndNum[i];
str18 = str17 + GenParityBit(str17);
tempID.Add(str18);
}
return tempID;
}

private bool isDiscValid(string s)
{
bool iFlag = false;
foreach (clsAdministrativeDivisionsCode cadc in cityCode)
{
if (cadc.AdCode == s)
{
string st = s.Substring(0, 2);
foreach (clsAdministrativeDivisionsCode capc in provinceCode)
{
if (capc.AdCode.Substring(0, 2) == st)
{
lbCardInfo.Items.Add("省或直辖市名称:" + capc.AdName);
break;
}
}

foreach (clsAdministrativeDivisionsCode caac in cityCode)
{
if (caac.AdCode == s.Substring(0, 4) + "00")
{

lbCardInfo.Items.Add("市名称:" + caac.AdName.TrimStart());
break;
}
}

lbCardInfo.Items.Add("地区名称:" + cadc.AdName.TrimStart());
iFlag = true;
break;
}
}
return iFlag;
}

private bool isBirthValid(string s)
{
string sYear = s.Substring(0, 4);
string sMonth = s.Substring(4, 2);
string sDay = s.Substring(6, 2);
DateTime dt;
if (DateTime.TryParse(string.Format("{0}-{1}-{2}", sYear, sMonth, sDay), out dt))
{
if (dt > DateTime.Now)
return false;
else
return true;
}
else
return false;

}

private bool isSexValid(string s)
{
return s == "000" ? false : true;
}

private bool isParityValid(string s18)
{
int[] Weight = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
string Parity = "10X98765432";
string s17 = s18.Substring(0, 17);
int s = 0;
for (int i = 0; i < s17.Length; i++)
{
s += Int32.Parse(s17[i].ToString()) * Weight[i];
}
return Parity[s % 11].ToString() == s18.Substring(17, 1) ? true : false;
}

#endregion

界面演示如下:

ID INTERFACE

C#实现的18位身份证号码最新算法[验证部分]

这里是主要的验证部分,如有需要全部源代码的,请在评论中留下你的E-MAIL。

C#实现的18位身份证号码最新算法[验证部分]


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论