身份证验证方案

     经常发现很多的学生都需要用到身份证验证,但验证过程总是判断是否是17位数字+X字母或15位数字,数据本身并没有达到真正的有效性处理,例如:输入333333333333333333,这个验证肯定是通过的,但试想一下,有33月,33日吗?

     为了让大家更好的处理身份证验证,写了一个案例,如下:

     1.窗体设计代码

      namespace Cases2 { partial class FrmValidataPID { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.btnValidate = new System.Windows.Forms.Button(); this.txtValidateContent = new System.Windows.Forms.TextBox(); this.lblValidateContent = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btnValidate // this.btnValidate.Location = new System.Drawing.Point(267, 86); this.btnValidate.Name = "btnValidate"; this.btnValidate.Size = new System.Drawing.Size(74, 25); this.btnValidate.TabIndex = 5; this.btnValidate.Text = "验证"; this.btnValidate.UseVisualStyleBackColor = true; this.btnValidate.Click += new System.EventHandler(this.btnValidate_Click); // // txtValidateContent // this.txtValidateContent.Location = new System.Drawing.Point(113, 40); this.txtValidateContent.Name = "txtValidateContent"; this.txtValidateContent.Size = new System.Drawing.Size(228, 23); this.txtValidateContent.TabIndex = 4; // // lblValidateContent // this.lblValidateContent.AutoSize = true; this.lblValidateContent.Location = new System.Drawing.Point(16, 43); this.lblValidateContent.Name = "lblValidateContent"; this.lblValidateContent.Size = new System.Drawing.Size(77, 14); this.lblValidateContent.TabIndex = 3; this.lblValidateContent.Text = "验证内容:"; // // FrmValidataPID // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(357, 135); this.Controls.Add(this.btnValidate); this.Controls.Add(this.txtValidateContent); this.Controls.Add(this.lblValidateContent); this.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "FrmValidataPID"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "验证身份证号码"; this.ResumeLayout(false); this.PerformLayout(); } #endregion protected System.Windows.Forms.Button btnValidate; protected System.Windows.Forms.TextBox txtValidateContent; protected System.Windows.Forms.Label lblValidateContent; } }

     2.后台代码

     using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; namespace Cases2 { public partial class FrmValidataPID : Form { public FrmValidataPID() { InitializeComponent(); } private void btnValidate_Click(object sender, EventArgs e) { CheckPIDCard(this.txtValidateContent.Text); } public bool CheckPIDCard(string sCardID) { //身份证号码校验函数 string ereg = null; int year = 0; Regex regex = null; List<string> errors = new List<string>(); errors.AddRange(new string[] { "验证通过!", "身份证号码位数不对!", "身份证号码出生日期超出范围或含有非法字符!", "身份证号码校验错误!", "身份证地区非法!" }); Dictionary<int, string> area = new Dictionary<int, string>(); List<int> keys = new List<int>(); keys.AddRange(new int[] { 11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 50, 51, 52, 53, 54, 61, 62, 63, 64, 65, 71, 81, 82, 91 }); List<string> values = new List<string>(); values.AddRange(new string[] { "北京", "天津", "河北", "山西", "内蒙古", "辽宁", "吉林", "黑龙江", "上海", "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "广西", "海南", "重庆", "四川", "贵州", "云南", "西藏", "陕西", "甘肃", "青海", "宁夏", "新疆", "台湾", "香港", "澳门", "国外" }); char[] sCard = sCardID.ToCharArray(); List<string> sCardInfo = new List<string>(); foreach (char ch in sCard) { sCardInfo.Add(ch.ToString()); } for (int i = 0; i < keys.Count; i++) { area.Add(keys[i], values[i]); } //地区检验 string sProvinceID = sCardID.Substring(0, 2); bool flag = false; foreach (int key in area.Keys) { if (key == Convert.ToInt32(sProvinceID)) { flag = true; } } if (!flag) { MessageBox.Show(this, errors[4], "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } //身份号码倍数及格式检验 switch (sCardID.Length) { case 15: year = Convert.ToInt32(sCardID.Substring(6, 2)); if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { ereg = @"/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/"; } else { ereg = @"/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/"; } regex = new Regex(ereg); if (!regex.IsMatch(sCardID)) { MessageBox.Show(this, errors[2], "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } else { break; } case 18: year = Convert.ToInt32(sCardID.Substring(6, 4)); if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { ereg = @"/^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/"; } else { ereg = @"/^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/"; } regex = new Regex(ereg); if (!regex.IsMatch(sCardID)) { int S = (Convert.ToInt32(sCardInfo[0]) + Convert.ToInt32(sCardInfo[10])) * 7 + (Convert.ToInt32(sCardInfo[1]) + Convert.ToInt32(sCardInfo[11])) * 9 + (Convert.ToInt32(sCardInfo[2]) + Convert.ToInt32(sCardInfo[12])) * 10 + (Convert.ToInt32(sCardInfo[3]) + Convert.ToInt32(sCardInfo[13])) * 5 + (Convert.ToInt32(sCardInfo[4]) + Convert.ToInt32(sCardInfo[14])) * 8 + (Convert.ToInt32(sCardInfo[5]) + Convert.ToInt32(sCardInfo[15])) * 4 + (Convert.ToInt32(sCardInfo[6]) + Convert.ToInt32(sCardInfo[16])) * 2 + Convert.ToInt32(sCardInfo[7]) * 1 + Convert.ToInt32(sCardInfo[8]) * 6 + Convert.ToInt32(sCardInfo[9]) * 3; int Y = S % 11; string M = "F"; string JYM = "10X98765432"; M = JYM.Substring(Y, 1);//判断校验位 if (M == sCardInfo[17]) { return true; } else { MessageBox.Show(this, errors[3], "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } else { MessageBox.Show(this, errors[2], "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } default: MessageBox.Show(this, errors[1], "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } return true; } } }

     希望能通过这个应用程序案例给大家带来解决身份证有效性验证的烦恼。

你可能感兴趣的:(身份证验证方案)