• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
yoxking
yoxking Tech.
博客园    首页    新随笔    联系   管理    订阅  订阅

C# 身份证号的较验表达式

声明:以下代码从csdn的网友下载

<%@ WebService Language="c#"  Class="WeatherWS.checkCID" %>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WeatherWS
{
/// <summary>
/// checkID 的摘要说明。
/// </summary>
[WebService(Namespace="http://flying.redv.com/monster")]
public class checkCID : System.Web.Services.WebService
{
public checkCID()
{
InitializeComponent();
}

#region 组件设计器生成的代码

//Web 服务设计器所必需的
private IContainer components = null;

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion

[WebMethod(Description="输入15或18位身份证号码以验证其有效性")]
public idDataClass CheckCidInfo(string cid)
{
string[] aCity = new string[]{null,null,null,null,null,null,null,null,null,null,null,"北京","天津","河北","山西","内蒙古",null,null,null,null,null,"辽宁","吉林","黑龙江",null,null,null,null,null,null,null,"上海","江苏","浙江","安微","福建","江西","山东",null,null,null,"河南","湖北","湖南","广东","广西","海南",null,null,null,"重庆","四川","贵州","云南","西藏",null,null,null,null,null,null,"陕西","甘肃","青海","宁夏","新疆",null,null,null,null,null,"台湾",null,null,null,null,null,null,null,null,null,"香港","澳门",null,null,null,null,null,null,null,null,"国外"};
double iSum=0;
idDataClass _cidData = new idDataClass();
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$|^\d{15}$");
System.Text.RegularExpressions.Match mc = rg.Match(cid);
if(!mc.Success)
{
_cidData.isValid = false;
_cidData.chkInfo = "不是有效的身份证号";
return _cidData;
}
if(cid.Length==15) cid = this.convert15to18(cid);
cid = cid.ToLower();
cid = cid.Replace("x","a");
if(aCity[int.Parse(cid.Substring(0,2))]==null)
{
_cidData.isValid = false;
_cidData.chkInfo = "非法地区";
return _cidData;
}
try
{
DateTime.Parse(cid.Substring(6,4)+"-"+cid.Substring(10,2)+"-"+cid.Substring(12,2));
}
catch
{
_cidData.isValid = false;
_cidData.chkInfo = "非法生日";
return _cidData;
}
for(int i=17;i>=0;i--)
{   
iSum +=(System.Math.Pow(2,i)%11)*int.Parse(cid[17-i].ToString(),System.Globalization.NumberStyles.HexNumber);

}
if(iSum%11!=1)
{
_cidData.isValid = false;
_cidData.chkInfo = "非法证号";
return _cidData;
}

_cidData.isValid = true;
_cidData.chkInfo = aCity[int.Parse(cid.Substring(0,2))]+","+cid.Substring(6,4)+"-"+cid.Substring(10,2)+"-"+cid.Substring(12,2)+","+(int.Parse(cid.Substring(16,1))%2==1?"男":"女");
return _cidData;
}
//将15位身份证号码转换为18位
private string convert15to18(string cid)
{
char[] strJiaoYan = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
int[] intQuan = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
string strTemp;
int intTemp = 0;

strTemp = cid.Substring(0,6) + "19" + cid.Substring(6);
for (int i=0;i<=strTemp.Length-1;i++)
{
intTemp += int.Parse(strTemp.Substring(i,1))*intQuan[i];
}
intTemp = intTemp % 11;
return strTemp + strJiaoYan[intTemp];
}

}
//用于保存身份证验证结果的类
public class idDataClass
{
public idDataClass()
{
}
public bool isValid;
public string chkInfo;
}
}

 

posted @ 2006-06-07 16:22  yoxking   阅读(405)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3