網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
實(shí)踐過(guò)程
效果
代碼
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string strg;//數(shù)據(jù)庫(kù)路徑
OleDbConnection conn;//數(shù)據(jù)連接對(duì)象
OleDbCommand cmd;//OleDbCommand對(duì)象
OleDbDataReader sdr;//OleDbDataReader對(duì)象
private bool CheckCard(string cardId)//創(chuàng)建一個(gè)CheckCard方法用于檢查身份證號(hào)碼是否合法
{
if (cardId.Length == 18) //如果身份證號(hào)為18位
{
return CheckCard18(cardId);//調(diào)用CheckCard18方法驗(yàn)證
}
else if (cardId.Length == 15) //如果身份證號(hào)為15位
{
return CheckCard15(cardId);//調(diào)用CheckCard15方法驗(yàn)證
}
else
{
return false;
}
}
private bool CheckCard18(string CardId)//CheckCard18方法用于檢查18位身份證號(hào)碼的合法性
{
long n = 0;
bool flag = false;
if (long.TryParse(CardId.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(CardId.Replace('x', '0').Replace('X', '0'), out n) == false)
return false;//數(shù)字驗(yàn)證
string[] Myaddress =new string[]{ "11","22","35","44","53","12",
"23","36","45","54","13","31","37","46","61","14","32","41",
"50","62","15","33","42","51","63","21","34","43","52","64",
"65","71","81","82","91"};
for (int kk = 0; kk < Myaddress.Length;kk++ )
{
if (Myaddress[kk].ToString() == CardId.Remove(2))
{
flag = true;
}
}
if (flag)
{
return flag;
}
string Mybirth = CardId.Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime Mytime = new DateTime();
if (DateTime.TryParse(Mybirth, out Mytime) == false)
return false;//生日驗(yàn)證
string[] MyVarifyCode = ("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 = CardId.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
sum += int.Parse(wi[i]) * int.Parse(ai[i].ToString());
int y = -1;
Math.DivRem(sum, 11, out y);
if (MyVarifyCode[y] != CardId.Substring(17, 1).ToLower())
{
return false;//校驗(yàn)碼驗(yàn)證
}
return true;//符合GB11643-1999標(biāo)準(zhǔn)
}
private bool CheckCard15(string CardId)
{
long n = 0;
bool flag = false;
if (long.TryParse(CardId, out n) == false || n < Math.Pow(10, 14))
return false;//數(shù)字驗(yàn)證
string[] Myaddress = new string[]{ "11","22","35","44","53","12",
"23","36","45","54","13","31","37","46","61","14","32","41",
"50","62","15","33","42","51","63","21","34","43","52","64",
"65","71","81","82","91"};
for (int kk = 0; kk < Myaddress.Length; kk++)
{
if (Myaddress[kk].ToString() == CardId.Remove(2))
{
flag = true;
}
}
if (flag)
{
return flag;
}
string Mybirth = CardId.Substring(6, 6).Insert(4, "-").Insert(2, "-");
DateTime Mytime = new DateTime();
if (DateTime.TryParse(Mybirth, out Mytime) == false)
{
return false;//生日驗(yàn)證
}
return true;//符合15位身份證標(biāo)準(zhǔn)
}
private void button1_Click(object sender, EventArgs e)
{
if (txtCardID.Text == "")//如果沒(méi)有輸入身份證號(hào)碼
{
return; //不執(zhí)行操作
}
else
{
if (CheckCard(txtCardID.Text.Trim()))//如果通過(guò)CheckCard方法驗(yàn)證成功
{
this.Height = 237; //設(shè)置窗體高度
string card=txtCardID.Text.Trim();//獲取輸入的身份證號(hào)碼
if (card.Length == 15) //如果輸入的是15位的身份證號(hào)碼,需要將其轉(zhuǎn)換成18位
{
int[] w = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
char[] a = new char[] { '1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2' };
string newID = "";
int s = 0;
newID =this.txtCardID.Text.Trim().Insert(6, "19");
for (int i = 0; i < 17; i++)
{
int k = Convert.ToInt32(newID[i]) * w[i];
s = s + k;
}
int h = 0;
Math.DivRem(s, 11, out h);
newID = newID + a[h];
card = newID; //最后將轉(zhuǎn)換成18位的身份證號(hào)碼賦值給card
}
int addnum =Convert.ToInt32(card.Remove(6));//獲取身份證號(hào)碼中的地址碼
//連接數(shù)據(jù)庫(kù)
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + strg);
conn.Open();//打開(kāi)數(shù)據(jù)庫(kù)
//查找數(shù)據(jù)庫(kù)中是否存在輸入的身份證號(hào)碼中的地址碼
cmd = new OleDbCommand("select count(*) from address where AddNum="+addnum, conn);
int KK = Convert.ToInt32(cmd.ExecuteScalar());
if (KK > 0)//如果存在
{
//檢索數(shù)據(jù)庫(kù)
cmd = new OleDbCommand("select * from address where AddNum=" + addnum, conn);
//實(shí)例化OleDbDataReader對(duì)象
sdr = cmd.ExecuteReader();
sdr.Read();//讀取該對(duì)象
string address = sdr["AddName"].ToString();//獲取地址碼對(duì)應(yīng)的歸屬地
string birthday = card.Substring(6, 8);//從身份證號(hào)碼中截取出公民的生日
string byear = birthday.Substring(0,4);//獲取出生年份
string bmonth = birthday.Substring(4,2);//獲取出生月份
if (bmonth.Substring(0, 1) == "0")//如果月份是以0開(kāi)頭
{
bmonth = bmonth.Substring(1,1);//去掉0
}
string bday = birthday.Substring(6,2);//獲取出生“日”
if (bday.Substring(0, 1) == "0")//如果“日”以0開(kāi)頭
{
bday = bday.Substring(1, 1);//去掉0
}
string sex = "";//性別
if (txtCardID.Text.Trim().Length == 15)//如果輸入的身份證號(hào)碼是15位
{
int PP=Convert.ToInt32(txtCardID.Text.Trim().Substring(14,1))%2;//判斷最后一位是奇數(shù)還是偶數(shù)
if (PP == 0)//如果是偶數(shù)
{
sex = "女";//說(shuō)明身份證號(hào)碼的持有者是女性
}
else
{
sex = "男";//如果是奇數(shù)則身份證號(hào)碼的持有者是男性
}
}
if (txtCardID.Text.Trim().Length == 18)//如果輸入的身份證號(hào)碼是18位
{
int PP = Convert.ToInt32(txtCardID.Text.Trim().Substring(16, 1)) % 2;//判斷倒數(shù)第二位是奇數(shù)還是偶數(shù)
if (PP == 0)//如果是偶數(shù)
{
sex = "女";//說(shuō)明身份證號(hào)碼的持有者是女性
}
else
{
sex = "男";//如果是奇數(shù)則身份證號(hào)碼的持有者是男性
}
}
sdr.Close();//關(guān)閉OleDbDataReader連接
conn.Close();//關(guān)閉數(shù)據(jù)庫(kù)連接
lblAddress.Text = address;//顯示身份證持有者的歸屬地
lblbirthday.Text = byear + "年" + bmonth + "月" + bday + "日";//顯示身份證持有者的生日
lblsex.Text = sex;//顯示身份證持有者的性別
lblresult.Text = "合法的公民身份證號(hào)!";//顯示驗(yàn)證結(jié)果
}
else
{
MessageBox.Show("公民身份證號(hào)輸入有誤!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("非法公民身份證號(hào)!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
txtCardID.Text = "";//清空輸入框
this.Height = 78; //設(shè)置窗體高度位78
}
private void Form1_Load(object sender, EventArgs e)
{
this.Height = 78;//設(shè)置窗體高度
//獲取數(shù)據(jù)庫(kù)路徑
strg = Application.StartupPath.ToString();
strg = strg.Substring(0, strg.LastIndexOf("\\"));
strg = strg.Substring(0, strg.LastIndexOf("\\"));
strg += @"\db.mdb";
}
}
partial class Form1
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtCardID = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = 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();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(65, 12);
this.label1.TabIndex = 0;
this.label1.Text = "身份證號(hào):";
//
// txtCardID
//
this.txtCardID.Location = new System.Drawing.Point(76, 10);
this.txtCardID.Name = "txtCardID";
this.txtCardID.Size = new System.Drawing.Size(207, 21);
this.txtCardID.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(289, 9);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "開(kāi)始驗(yàn)證";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(367, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "重新驗(yàn)證";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(13, 53);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(65, 12);
this.label2.TabIndex = 4;
this.label2.Text = "所屬地區(qū):";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(37, 90);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(41, 12);
this.label3.TabIndex = 5;
this.label3.Text = "生日:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(37, 129);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(41, 12);
this.label4.TabIndex = 6;
this.label4.Text = "性別:";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(13, 169);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(65, 12);
this.label5.TabIndex = 7;
this.label5.Text = "驗(yàn)證結(jié)果:";
//
// lblAddress
//
this.lblAddress.AutoSize = true;
this.lblAddress.Location = new System.Drawing.Point(74, 53);
this.lblAddress.Name = "lblAddress";
this.lblAddress.Size = new System.Drawing.Size(0, 12);
this.lblAddress.TabIndex = 8;
//
// lblbirthday
//
this.lblbirthday.AutoSize = true;
this.lblbirthday.Location = new System.Drawing.Point(74, 90);
this.lblbirthday.Name = "lblbirthday";
this.lblbirthday.Size = new System.Drawing.Size(0, 12);
this.lblbirthday.TabIndex = 9;
//
// lblsex
//
this.lblsex.AutoSize = true;
this.lblsex.Location = new System.Drawing.Point(74, 129);
this.lblsex.Name = "lblsex";
this.lblsex.Size = new System.Drawing.Size(0, 12);
this.lblsex.TabIndex = 10;
//
// lblresult
//
this.lblresult.AutoSize = true;
this.lblresult.Location = new System.Drawing.Point(74, 169);
this.lblresult.Name = "lblresult";
this.lblresult.Size = new System.Drawing.Size(0, 12);
this.lblresult.TabIndex = 11;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(449, 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.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtCardID);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "驗(yàn)證身份證號(hào)碼";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtCardID;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label lblAddress;
private System.Windows.Forms.Label lblbirthday;
private System.Windows.Forms.Label lblsex;
private System.Windows.Forms.Label lblresult;
}
原文鏈接:https://blog.csdn.net/qq_27489007/article/details/128306627
相關(guān)推薦
- 2022-09-20 Python?pip超詳細(xì)教程之pip的安裝與使用_python
- 2022-12-04 Flutter狀態(tài)管理Provider的使用示例詳解_Android
- 2022-07-07 Pandas提高數(shù)據(jù)分析效率的13個(gè)技巧匯總_python
- 2022-08-02 淺談Redis常見(jiàn)延遲問(wèn)題定位與分析_Redis
- 2024-07-15 bootspring第三方資源配置管理
- 2022-11-10 C++?TCP網(wǎng)絡(luò)編程詳細(xì)講解_C 語(yǔ)言
- 2022-06-11 在Docker容器中部署MSSQL_docker
- 2022-05-24 C++中的Qt?QTableView詳解_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支