目录
实践过程
效果
代码
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
string strg;
OleDbConnection conn;
OleDbCommand cmd;
OleDbDataReader sdr;
private bool CheckCard(string cardId)
{
if (cardId.Length ==)
{
return CheckCard(cardId);
}
else if (cardId.Length ==)
{
return CheckCard(cardId);
}
else
{
return false;
}
}
private bool CheckCard(string CardId)
{
long n =;
bool flag = false;
if (long.TryParse(CardId.Remove(), out n) == false || n < Math.Pow(10, 16) || long.TryParse(CardId.Replace('x', '0').Replace('X', '0'), out n) == false)
return false;
string[] Myaddress =new string[]{ "","22","35","44","53","12",
"","36","45","54","13","31","37","46","61","14","32","41",
"","62","15","33","42","51","63","21","34","43","52","64",
"","71","81","82","91"};
for (int kk =; kk < Myaddress.Length;kk++ )
{
if (Myaddress[kk].ToString() == CardId.Remove())
{
flag = true;
}
}
if (flag)
{
return flag;
}
string Mybirth = CardId.Substring(, 8).Insert(6, "-").Insert(4, "-");
DateTime Mytime = new DateTime();
if (DateTime.TryParse(Mybirth, out Mytime) == false)
return false;
string[] MyVarifyCode = (",0,x,9,8,7,6,5,4,3,2").Split(',');
string[] wi = (",9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] ai = CardId.Remove().ToCharArray();
int sum =;
for (int i =; i < 17; i++)
sum += int.Parse(wi[i]) * int.Parse(ai[i].ToString());
int y = -;
Math.DivRem(sum,, out y);
if (MyVarifyCode[y] != CardId.Substring(, 1).ToLower())
{
return false;
}
return true;
}
private bool CheckCard(string CardId)
{
long n =;
bool flag = false;
if (long.TryParse(CardId, out n) == false || n < Math.Pow(, 14))
return false;
string[] Myaddress = new string[]{ "","22","35","44","53","12",
"","36","45","54","13","31","37","46","61","14","32","41",
"","62","15","33","42","51","63","21","34","43","52","64",
"","71","81","82","91"};
for (int kk =; kk < Myaddress.Length; kk++)
{
if (Myaddress[kk].ToString() == CardId.Remove())
{
flag = true;
}
}
if (flag)
{
return flag;
}
string Mybirth = CardId.Substring(, 6).Insert(4, "-").Insert(2, "-");
DateTime Mytime = new DateTime();
if (DateTime.TryParse(Mybirth, out Mytime) == false)
{
return false;
}
return true;
}
private void button_Click(object sender, EventArgs e)
{
if (txtCardID.Text == "")
{
return;
}
else
{
if (CheckCard(txtCardID.Text.Trim()))
{
this.Height =;
string card=txtCardID.Text.Trim();
if (card.Length ==)
{
int[] w = new int[] {, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
char[] a = new char[] { '', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2' };
string newID = "";
int s =;
newID =this.txtCardID.Text.Trim().Insert(, "19");
for (int i =; i < 17; i++)
{
int k = Convert.ToInt(newID[i]) * w[i];
s = s + k;
}
int h =;
Math.DivRem(s,, out h);
newID = newID + a[h];
card = newID;
}
int addnum =Convert.ToInt(card.Remove(6));
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB..0;Data source=" + strg);
conn.Open();
cmd = new OleDbCommand("select count(*) from address where AddNum="+addnum, conn);
int KK = Convert.ToInt(cmd.ExecuteScalar());
if (KK >)
{
cmd = new OleDbCommand("select * from address where AddNum=" + addnum, conn);
sdr = cmd.ExecuteReader();
sdr.Read();
string address = sdr["AddName"].ToString();
string birthday = card.Substring(, 8);
string byear = birthday.Substring(,4);
string bmonth = birthday.Substring(,2);
if (bmonth.Substring(, 1) == "0")
{
bmonth = bmonth.Substring(,1);
}
string bday = birthday.Substring(,2);
if (bday.Substring(, 1) == "0")
{
bday = bday.Substring(, 1);
}
string sex = "";
if (txtCardID.Text.Trim().Length ==)
{
int PP=Convert.ToInt(txtCardID.Text.Trim().Substring(14,1))%2;
if (PP ==)
{
sex = "女";
}
else
{
sex = "男";
}
}
if (txtCardID.Text.Trim().Length ==)
{
int PP = Convert.ToInt(txtCardID.Text.Trim().Substring(16, 1)) % 2;
if (PP ==)
{
sex = "女";
}
else
{
sex = "男";
}
}
sdr.Close();
conn.Close();
lblAddress.Text = address;
lblbirthday.Text = byear + "年" + bmonth + "月" + bday + "日";
lblsex.Text = sex;
lblresult.Text = "合法的公民身份证号!";
}
else
{
MessageBox.Show("公民身份证号输入有误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("非法公民身份证号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void button_Click(object sender, EventArgs e)
{
txtCardID.Text = "";
this.Height =;
}
private void Form_Load(object sender, EventArgs e)
{
this.Height =;
strg = Application.StartupPath.ToString();
strg = strg.Substring(, strg.LastIndexOf("\\"));
strg = strg.Substring(, strg.LastIndexOf("\\"));
strg += @"\db.mdb";
}
}
partial class Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.label = new System.Windows.Forms.Label();
this.txtCardID = new System.Windows.Forms.TextBox();
this.button = new System.Windows.Forms.Button();
this.button = new System.Windows.Forms.Button();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.lblAddress = new System.Windows.Forms.Label();
this.lblbirthday = new System.Windows.Forms.Label();
this.lblsex = new System.Windows.Forms.Label();
this.lblresult = new System.Windows.Forms.Label();
this.SuspendLayout();
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(13, 13);
this.label.Name = "label1";
this.label.Size = new System.Drawing.Size(65, 12);
this.label.TabIndex = 0;
this.label.Text = "身份证号:";
this.txtCardID.Location = new System.Drawing.Point(, 10);
this.txtCardID.Name = "txtCardID";
this.txtCardID.Size = new System.Drawing.Size(, 21);
this.txtCardID.TabIndex =;
this.button.Location = new System.Drawing.Point(289, 9);
this.button.Name = "button1";
this.button.Size = new System.Drawing.Size(75, 23);
this.button.TabIndex = 2;
this.button.Text = "开始验证";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button1_Click);
this.button.Location = new System.Drawing.Point(367, 8);
this.button.Name = "button2";
this.button.Size = new System.Drawing.Size(75, 23);
this.button.TabIndex = 3;
this.button.Text = "重新验证";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button2_Click);
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(13, 53);
this.label.Name = "label2";
this.label.Size = new System.Drawing.Size(65, 12);
this.label.TabIndex = 4;
this.label.Text = "所属地区:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(37, 90);
this.label.Name = "label3";
this.label.Size = new System.Drawing.Size(41, 12);
this.label.TabIndex = 5;
this.label.Text = "生日:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(37, 129);
this.label.Name = "label4";
this.label.Size = new System.Drawing.Size(41, 12);
this.label.TabIndex = 6;
this.label.Text = "性别:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(13, 169);
this.label.Name = "label5";
this.label.Size = new System.Drawing.Size(65, 12);
this.label.TabIndex = 7;
this.label.Text = "验证结果:";
this.lblAddress.AutoSize = true;
this.lblAddress.Location = new System.Drawing.Point(, 53);
this.lblAddress.Name = "lblAddress";
this.lblAddress.Size = new System.Drawing.Size(, 12);
this.lblAddress.TabIndex =;
this.lblbirthday.AutoSize = true;
this.lblbirthday.Location = new System.Drawing.Point(, 90);
this.lblbirthday.Name = "lblbirthday";
this.lblbirthday.Size = new System.Drawing.Size(, 12);
this.lblbirthday.TabIndex =;
this.lblsex.AutoSize = true;
this.lblsex.Location = new System.Drawing.Point(, 129);
this.lblsex.Name = "lblsex";
this.lblsex.Size = new System.Drawing.Size(, 12);
this.lblsex.TabIndex =;
this.lblresult.AutoSize = true;
this.lblresult.Location = new System.Drawing.Point(, 169);
this.lblresult.Name = "lblresult";
this.lblresult.Size = new System.Drawing.Size(, 12);
this.lblresult.TabIndex =;
this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, 192);
this.Controls.Add(this.lblresult);
this.Controls.Add(this.lblsex);
this.Controls.Add(this.lblbirthday);
this.Controls.Add(this.lblAddress);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.label);
this.Controls.Add(this.button);
this.Controls.Add(this.button);
this.Controls.Add(this.txtCardID);
this.Controls.Add(this.label);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "验证身份证号码";
this.Load += new System.EventHandler(this.Form_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox txtCardID;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label lblAddress;
private System.Windows.Forms.Label lblbirthday;
private System.Windows.Forms.Label lblsex;
private System.Windows.Forms.Label lblresult;
}