实例介绍
【实例简介】
基于VS15 的 C# .NET Framework 4.5 用于查询身份证的籍贯、签发机关等基本信息,输入不正确时会提示出错误原因
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BMSIdCard
{
public partial class Form1copy : Form
{
IDS[] id = new IDS[4400];//
List<CardArea> objList = new List<CardArea>();
private string yob; //出生年份 算取生肖
private string born; //出生月份 与出生日相结合算取星座
private string day; //出生日期
public Form1copy()
{
//Substring 截取字符串
InitializeComponent();
//不可编辑的项 此处为根据输入的身份证号获取,计算的结果
this.txtsex.Enabled = false;
this.txtbirthyear.Enabled = false;
this.txtbirthmonth.Enabled = false;
this.txtbirthdate.Enabled = false;
this.txtarea.Enabled = false;
this.txtvalidatecode.Enabled = false;
this.txtconstellation.Enabled = false;
this.txtzodiac.Enabled = false;
this.txtage.Enabled = false;
this.txtplace.Enabled = false;
this.txtauthority.Enabled = false;
//文本字体颜色
txtidcard.ForeColor = txtage.ForeColor = txtarea.ForeColor = txtauthority.ForeColor = txtbirthdate.ForeColor
= txtbirthmonth.ForeColor = txtbirthyear.ForeColor = txtconstellation.ForeColor = txtzodiac.ForeColor =
txtplace.ForeColor = txtvalidatecode.ForeColor = txtsex.ForeColor = System.Drawing.Color.Black;
}
private void Form1copy_Load(object sender, EventArgs e)
{
Helper helper = new Helper();
helper.Show();
MessageBox.Show("请查看弹出的帮助提示窗体");
//IO 读取csv文件(签发机关、地区编码、籍贯)
//StreamReader sr = new StreamReader(Application.StartupPath "\\inif.csv", Encoding.Default);
StreamReader sr = new StreamReader("D:\\NHCJ\\BMSIdCard\\BMSIdCard\\inif.csv", Encoding.Default);
string str = "";
string a, b, c, d, e2;
string[] str_temp;
int index = 0;
while (!sr.EndOfStream)
{
str = sr.ReadLine().Trim();
str_temp = str.Split(',');
a = str_temp[0].Trim();
b = str_temp[1].Trim();
c = str_temp[2].Trim();
d = str_temp[3].Trim();
e2 = str_temp[4].Trim();
id[index ] = new IDS(a, b, c, d, e2);
}
sr.Close();
}
public struct IDS
{
public string dmmc;
public string dmzm;
public string dmbz;
public string dmxh;
public string dmmcl;
public IDS(string a, string b, string c, string d, string e)
{
this.dmmc = a; //签发机关 "公安局"
this.dmzm = b; //所在地编码
this.dmbz = c; //签发机关简拼 比如永城市 hnyc
this.dmxh = d; //编号
this.dmmcl = e; //籍贯
}
}
/// <summary>
/// 验证身份证号码
/// 获取生日、籍贯、签发机关、生肖、星座、地区编码等
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_verify_Click(object sender, EventArgs e)
{
if (!ValidateInput(this.txtidcard.Text))
{
return;
}
else
{
string idcard = this.txtidcard.Text.Trim();
//string sexCode = string.Empty, areaCode = string.Empty, birthdayCode = string.Empty;
//areaCode = idcard.Substring(0, 6);
//birthdayCode = idcard.Substring(6, 8);
//sexCode = idcard.Substring(16, 1);
//展示信息
this.txtsex.Text = (int.Parse(idcard.Substring(16, 1)) % 2 == 0) ? "女" : "男";
//this.txtbirthyear.Text = idcard.Substring(6, 4) "-" idcard.Substring(10, 2) "-" idcard.Substring(12, 2);
this.txtbirthyear.Text = idcard.Substring(6, 4);
this.txtbirthmonth.Text = idcard.Substring(10, 2);
this.txtbirthdate.Text = idcard.Substring(12, 2);
#region 月份与日期核对验证
if (int.Parse(idcard.Substring(10, 2)) > 12)
{
txtidcard.ForeColor = System.Drawing.Color.Red;
txtbirthmonth.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("出生月份错误,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
txtidcard.ForeColor = System.Drawing.Color.Black;
txtbirthmonth.ForeColor = System.Drawing.Color.Black;
}
if (int.Parse(idcard.Substring(6, 4)) % 4 == 0 && int.Parse(idcard.Substring(6, 4)) % 100 != 0 || int.Parse(idcard.Substring(6, 4)) % 400 == 0)
{
if (int.Parse(idcard.Substring(10, 2)) == 02 && int.Parse(idcard.Substring(12, 2)) > 29)
{
txtidcard.ForeColor = System.Drawing.Color.Red;
txtbirthdate.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("闰年02月出生日不可大于29天,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
txtidcard.ForeColor = System.Drawing.Color.Black;
txtbirthdate.ForeColor = System.Drawing.Color.Black;
}
}
else
{
if (int.Parse(idcard.Substring(10, 2)) == 02 && int.Parse(idcard.Substring(12, 2)) > 28)
{
txtidcard.ForeColor = System.Drawing.Color.Red;
txtbirthdate.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("平年02月出生日不可大于28天,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
txtidcard.ForeColor = System.Drawing.Color.Black;
txtbirthdate.ForeColor = System.Drawing.Color.Black;
}
}
if (int.Parse(idcard.Substring(10, 2)) == 04 || int.Parse(idcard.Substring(10, 2)) == 06 || int.Parse(idcard.Substring(10, 2)) == 09 || int.Parse(idcard.Substring(10, 2)) == 11)
{
if (int.Parse(idcard.Substring(12, 2)) > 30)
{
txtidcard.ForeColor = System.Drawing.Color.Red;
txtbirthdate.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("每年04、06、09、11月份出生日不可大于30天,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
txtidcard.ForeColor = System.Drawing.Color.Black;
txtbirthdate.ForeColor = System.Drawing.Color.Black;
}
}
else
{
if (int.Parse(idcard.Substring(12, 2)) > 31)
{
txtidcard.ForeColor = System.Drawing.Color.Red;
txtbirthdate.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("每年01、03、05、07、08、10、12月份出生日不可大于31天,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
txtidcard.ForeColor = System.Drawing.Color.Black;
txtbirthdate.ForeColor = System.Drawing.Color.Black;
}
}
#endregion
this.txtzodiac.Text = zodiac(yob); //生肖
this.txtconstellation.Text = constellation(born, day); //星座
//this.txtarea.Text = GetAreaName(idcard.Substring(0, 6));
this.txtarea.Text = idcard.Substring(0, 6); //地区编码
///簽發機關,籍貫
string keys = txtidcard.Text;
string dmzm = keys.Substring(0, 6);
bool flag = false;
foreach (IDS i in id)
{
if (i.dmzm.Equals(dmzm))
{
flag = true;
this.txtauthority.Text = i.dmmc "公安局"; //签发机关
this.txtplace.Text = i.dmmcl; //籍贯
break;
}//if
}
//算取年龄
string sBirthday = idcard.Substring(6, 8);//获取取生日
DateTime dtBirthday = DateTime.Parse(sBirthday.Substring(0, 4) "-" sBirthday.Substring(4, 2) "-" sBirthday.Substring(6, 2));
//TimeSpan sp = DateTime.Now.Subtract(dtBirthday);
//this.txtage.Text = (sp.TotalDays / 365).ToString(); //算取详细年龄 保留所有小数
TimeSpan ts = DateTime.Now.Subtract(Convert.ToDateTime(dtBirthday));
this.txtage.Text = Math.Floor(ts.TotalDays / 365).ToString(); //算取年龄 向小取整
this.txtvalidatecode.Text = CalculateValidateCode(idcard);
if (CalculateValidateCode(idcard) != idcard.Substring(17, 1))
{
txtidcard.ForeColor = System.Drawing.Color.Red;
txtvalidatecode.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("验证码错误,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
txtidcard.ForeColor = System.Drawing.Color.Black;
txtvalidatecode.ForeColor = System.Drawing.Color.Black;
}
}
}
/// <summary>
/// 输入身份证号验证 身份证号必须是18位二代身份证
/// </summary>
/// <param name="inputString"></param>
/// <returns></returns>
private bool ValidateInput(string inputString)
{
if (string.IsNullOrWhiteSpace(inputString))
{
MessageBox.Show("输入的身份证号码不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtidcard.Focus();
return false;
}
if (inputString.Trim().Length != 18)
{
txtidcard.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("输入的身份证号码必须是18位,已用红色标注,请修改!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtidcard.Focus();
this.txtidcard.SelectAll();
return false;
}
else
{
txtidcard.ForeColor = System.Drawing.Color.Black;
return true;
}
}
/// <summary>
/// 身份证校验码验证
/// </summary>
/// <param name="inputstring"></param>
/// <returns></returns>
private string CalculateValidateCode(string inputstring)
{
byte[] Validate = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
byte[] Idcard = new byte[17];
int[] multiplication = new int[17];
for (int i = 0; i < inputstring.Trim().Length-1; i )
{
Idcard[i] = byte.Parse(inputstring[i].ToString());
multiplication[i] = (byte)(Validate[i] * Idcard[i]);
}
switch (multiplication.Sum() % 11)
{
case 0:
return "1";
case 1:
return "0";
case 2:
return "X";
case 3:
return "9";
case 4:
return "8";
case 5:
return "7";
case 6:
return "6";
case 7:
return "5";
case 8:
return "4";
case 9:
return "3";
case 10:
return "2";
default:
return null;
//break;
}
}
/// <summary>
/// 所属生肖
/// </summary>
/// <param name="y"></param>
/// <returns></returns>
public string zodiac(string yob)
{
yob = this.txtbirthyear.Text;
uint temp_y = uint.Parse(yob);
temp_y = (temp_y - 1900) % 12;
switch (temp_y)
{
case 0:
return "鼠";
break;
case 1:
return "牛";
break;
case 2:
return "虎";
break;
case 3:
return "兔";
break;
case 4:
return "龙";
break;
case 5:
return "蛇";
break;
case 6:
return "马";
break;
case 7:
return "羊";
break;
case 8:
return "猴";
break;
case 9:
return "鸡";
break;
case 10:
return "狗";
break;
case 11:
return "猪";
break;
default:
return "not found";
break;
}
//鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪
}
/// <summary>
/// 星座
/// </summary>
/// <param name="born"></param>
/// <param name="day"></param>
/// <returns></returns>
public string constellation(string born, string day)
{
born = this.txtbirthmonth.Text;
day = this.txtbirthdate.Text;
if (born == "01")
{
if (int.Parse(day) <= 19)
return "水瓶座";
else
return "水瓶座";
}
else if (born == "02")
{
if (int.Parse(day) <= 19)
return "水瓶座";
else
return "双鱼座";
}
else if (born == "03")
{
if (int.Parse(day) <= 20)
return "双鱼座";
else
return "白羊座";
}
else if (born == "04")
{
if (int.Parse(day) <= 20)
return "白羊座";
else
return "金牛座";
}
else if (born == "05")
{
if (int.Parse(day) <= 20)
return "金牛座";
else
return "双子座";
}
else if (born == "06")
{
if (int.Parse(day) <= 21)
return "双子座";
else
return "巨蟹座";
}
else if (born == "07")
{
if (int.Parse(day) <= 22)
return "巨蟹座";
else
return "狮子座";
}
else if (born == "08")
{
if (int.Parse(day) <= 22)
return "狮子座";
else
return "处女座";
}
else if (born == "09")
{
if (int.Parse(day) <= 22)
return "处女座";
else
return "天平座";
}
else if (born == "10")
{
if (int.Parse(day) <= 22)
return "天平座";
else
return "天蝎座";
}
else if (born == "11")
{
if (int.Parse(day) <= 21)
return "天蝎座";
else
return "射手座";
}
else
{
if (int.Parse(day) <= 21)
return "射手座";
else
return "魔羯座";
}
}
}
}
标签: 查询身份证号码籍贯等基本信息
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论