C#语言实例源码系列-身份证验证
创始人
2024-04-26 09:31:43
0
专栏分享
  • 点击跳转=>Unity3D特效百例
  • 点击跳转=>案例项目实战源码
  • 点击跳转=>游戏脚本-辅助自动化
  • 点击跳转=>Android控件全解手册

👉关于作者

众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣 !!!
专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
有什么需要欢迎私我,交流群让学习不再孤单

在这里插入图片描述

👉实践过程

😜效果

在这里插入图片描述

😜代码

public partial class Form1 : Form
{public Form1(){InitializeComponent();}string strg;//数据库路径OleDbConnection conn;//数据连接对象OleDbCommand cmd;//OleDbCommand对象OleDbDataReader sdr;//OleDbDataReader对象private bool CheckCard(string cardId)//创建一个CheckCard方法用于检查身份证号码是否合法{if (cardId.Length == 18)        //如果身份证号为18位{return  CheckCard18(cardId);//调用CheckCard18方法验证}else if (cardId.Length == 15)   //如果身份证号为15位{return CheckCard15(cardId);//调用CheckCard15方法验证}else{return false;}}private bool CheckCard18(string CardId)//CheckCard18方法用于检查18位身份证号码的合法性{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;//数字验证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;//生日验证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;//校验码验证}return true;//符合GB11643-1999标准}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;//数字验证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;//生日验证}return true;//符合15位身份证标准}private void button1_Click(object sender, EventArgs e){if (txtCardID.Text == "")//如果没有输入身份证号码{return;              //不执行操作}else{if (CheckCard(txtCardID.Text.Trim()))//如果通过CheckCard方法验证成功{this.Height = 237;               //设置窗体高度string card=txtCardID.Text.Trim();//获取输入的身份证号码if (card.Length == 15)          //如果输入的是15位的身份证号码,需要将其转换成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;               //最后将转换成18位的身份证号码赋值给card}int addnum =Convert.ToInt32(card.Remove(6));//获取身份证号码中的地址码//连接数据库conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + strg);conn.Open();//打开数据库//查找数据库中是否存在输入的身份证号码中的地址码cmd = new OleDbCommand("select count(*) from address where AddNum="+addnum, conn);int KK = Convert.ToInt32(cmd.ExecuteScalar());if (KK > 0)//如果存在{//检索数据库cmd = new OleDbCommand("select * from address where AddNum=" + addnum, conn);//实例化OleDbDataReader对象sdr = cmd.ExecuteReader();sdr.Read();//读取该对象string address = sdr["AddName"].ToString();//获取地址码对应的归属地string birthday = card.Substring(6, 8);//从身份证号码中截取出公民的生日string byear = birthday.Substring(0,4);//获取出生年份string bmonth = birthday.Substring(4,2);//获取出生月份if (bmonth.Substring(0, 1) == "0")//如果月份是以0开头{bmonth = bmonth.Substring(1,1);//去掉0}string bday = birthday.Substring(6,2);//获取出生“日”if (bday.Substring(0, 1) == "0")//如果“日”以0开头{bday = bday.Substring(1, 1);//去掉0}string sex = "";//性别if (txtCardID.Text.Trim().Length == 15)//如果输入的身份证号码是15位{int PP=Convert.ToInt32(txtCardID.Text.Trim().Substring(14,1))%2;//判断最后一位是奇数还是偶数if (PP == 0)//如果是偶数{sex = "女";//说明身份证号码的持有者是女性}else{sex = "男";//如果是奇数则身份证号码的持有者是男性}}if (txtCardID.Text.Trim().Length == 18)//如果输入的身份证号码是18位{int PP = Convert.ToInt32(txtCardID.Text.Trim().Substring(16, 1)) % 2;//判断倒数第二位是奇数还是偶数if (PP == 0)//如果是偶数{sex = "女";//说明身份证号码的持有者是女性}else{sex = "男";//如果是奇数则身份证号码的持有者是男性}}sdr.Close();//关闭OleDbDataReader连接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 button2_Click(object sender, EventArgs e){txtCardID.Text = "";//清空输入框this.Height = 78;   //设置窗体高度位78}private void Form1_Load(object sender, EventArgs e){this.Height = 78;//设置窗体高度//获取数据库路径strg = Application.StartupPath.ToString();strg = strg.Substring(0, strg.LastIndexOf("\\"));strg = strg.Substring(0, strg.LastIndexOf("\\"));strg += @"\db.mdb";}
}
partial class Form1
{/// /// 必需的设计器变量。/// private System.ComponentModel.IContainer components = null;/// /// 清理所有正在使用的资源。/// /// 如果应释放托管资源,为 true;否则为 false。protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// /// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// 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 = "身份证号:";// // 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 = "开始验证";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 = "重新验证";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 = "所属地区:";// // 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 = "验证结果:";// // 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 = "验证身份证号码";this.Load += new System.EventHandler(this.Form1_Load);this.ResumeLayout(false);this.PerformLayout();}#endregionprivate 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;}

需要的再直接Call我,直接发。

👉其他

📢作者:小空和小芝中的小空
📢转载说明-务必注明来源:https://zhima.blog.csdn.net/
📢这位道友请留步☁️,我观你气度不凡,谈吐间隐隐有王者霸气💚,日后定有一番大作为📝!!!旁边有点赞👍收藏🌟今日传你,点了吧,未来你成功☀️,我分文不取,若不成功⚡️,也好回来找我。

温馨提示点击下方卡片获取更多意想不到的资源。
空名先生

相关内容

热门资讯

安卓系统源码怎么打开,并可能需... 你有没有想过,安卓系统的源码就像是一扇神秘的门,隐藏着无数的技术秘密?想要打开这扇门,你得掌握一些小...
安卓8.0系统体验视频,智能革... 你有没有听说安卓8.0系统最近可是火得一塌糊涂啊!作为一个紧跟科技潮流的数码达人,我当然要来给你好好...
宣传系统漫画app安卓,探索安... 亲爱的读者们,你是否曾在某个午后,百无聊赖地打开手机,想要寻找一些轻松愉悦的读物?今天,我要给你介绍...
鸿蒙替换安卓系统吗,开启智能生... 你知道吗?最近科技圈里可是炸开了锅,因为华为的新操作系统鸿蒙系统,据说要大举进军手机市场,替换掉安卓...
手机安卓系统深度清理,解锁手机... 手机里的东西是不是越来越多,感觉就像一个装满了杂物的储物柜?别急,今天就来教你一招——手机安卓系统深...
安卓上的windows系统,融... 你有没有想过,在安卓手机上也能体验到Windows系统的魅力呢?没错,这就是今天我要跟你分享的神奇故...
安卓系统焦点变化事件,Andr... 你知道吗?在安卓系统的世界里,最近发生了一件超级有趣的事情——焦点变化事件。这可不是什么小打小闹,它...
一加系统安卓降级,轻松还原经典... 你有没有想过,你的手机系统升级后,突然发现某些功能变得不那么顺心了?别急,今天就来聊聊一加系统安卓降...
日本最好的安卓系统,体验非凡 亲爱的读者们,你是否曾想过,在遥远的东方,有一个国家,他们的智能手机系统独具特色,让人眼前一亮?没错...
荣耀安卓11 系统证书,保障安... 你知道吗?最近手机圈里可是炸开了锅,荣耀安卓11系统证书成了大家热议的话题。这不,我就迫不及待地来和...
安卓手机开机升级系统,体验流畅... 你有没有发现,每次你的安卓手机开机,总会有那么一刹那,屏幕上跳出一个升级系统的提示?是不是觉得这就像...
真正的安卓系统手机,安卓系统手... 你有没有想过,为什么有些人对安卓系统手机情有独钟?是不是觉得市面上的安卓手机千篇一律,缺乏个性?别急...
安卓怎么用定位系统,轻松实现精... 你有没有想过,手机里的定位系统竟然这么神奇?它不仅能帮你找到回家的路,还能在茫茫人海中找到你的好友。...
安卓的哪个系统流畅,探索新一代... 你有没有想过,为什么你的安卓手机有时候像蜗牛一样慢吞吞的,而别人的手机却像风一样快?今天,就让我带你...
安卓系统解锁工具下载,畅享自由 你是不是也和我一样,对安卓系统的解锁工具感兴趣呢?想象你的手机被锁住了,无论是忘记密码还是想尝试新的...
谷歌退出安卓系统停用,停用背后... 你知道吗?最近有个大新闻在科技圈里炸开了锅!谷歌竟然宣布要退出安卓系统,这可真是让人大跌眼镜啊!想象...
安卓系统卡顿修复,轻松提升手机... 手机用久了是不是感觉有点卡卡的呢?别急,今天就来给你支几招,让你的安卓手机重焕生机,告别卡顿的烦恼!...
安卓系统停用怎么解除,轻松恢复... 你是不是也遇到了安卓系统停用的问题,急得像热锅上的蚂蚁?别急,今天就来给你详细解析怎么解除这个让人头...
最初始的安卓系统,技术演进与产... 亲爱的读者,你是否曾好奇过,那个如今无处不在的安卓系统,它的诞生之初是怎样的呢?今天,就让我们一起穿...
patchwall系统和安卓系... 你有没有发现,手机上的界面越来越个性化了?没错,这就是科技的魅力所在。今天,咱们就来聊聊两个在个性化...